GoLang的结构

时间:2015-12-18 19:29:43

标签: struct go

我刚开始使用GoLang,我正在查看他们的一个教程(https://golang.org/doc/code.html)。

在他们的一个例子中,他们将一个变量设置为一个结构,但是我很困惑他们如何在下面的for循环中访问结构的元素?有人可以澄清吗?非常感谢!

代码:

package stringutil

import "testing"

func TestReverse(t *testing.T) {
    cases := []struct {
        in, want string
    }{
        {"Hello, world", "dlrow ,olleH"},
        {"Hello, 世界", "界世 ,olleH"},
        {"", ""},
    }
    for _, c := range cases {
        got := Reverse(c.in)
        if got != c.want {
            t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
        }
    }
}

3 个答案:

答案 0 :(得分:8)

下面是一些代码,其中包含一些注释,以帮助澄清每个语句中的角色。

import "testing"

func TestReverse(t *testing.T) {
    cases := []struct { // declaration of anonymous type
        in, want string // fields on that type called in and want, both strings
    }{
        {"Hello, world", "dlrow ,olleH"},
        {"Hello, 世界", "界世 ,olleH"},
        {"", ""},
    } // composite literal initilization
    // note the use of := in assigning to cases, that op combines declaration and assignment into one statement
    for _, c := range cases { // range over cases, ignoring the index - the underscore means to discard that return value
        got := Reverse(c.in) // c is the current instance, access in with the familiar dot notation
        if got != c.want { // again, access operator on c, the current instance
            t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want) // more access
        }
    }
}

如果有帮助,请告诉我。如果某些陈述仍然没有意义,我可以尝试用口语提供更多的摘要或添加更多细节。另外,如果您不熟悉某个集合的范围'范围',则返回k, v,其中k是索引或键,v是该值。

编辑:有关cases

的声明/启动的详细信息
    cases := []struct {
        in, want string
    }

第一对花括号内的这一位是结构的定义。这是一个匿名类型,正常声明看起来像这样;

    type case strcut {
        in string
        want string
    }

如果你有这样的东西,那么在这个包的范围内会有一个名为case的类型(如果你想让它'公开'则不会导出,所以它需要{{1}而不是)。相反,examples struct是匿名的。它与普通类型的工作方式相同,但作为开发人员,您将无法引用该类型,因此您只能使用此处初始化的集合。在内部,此类型与具有2个未导出字段的字符串的任何其他结构相同。这些字段的名称为type Casein。请注意,在want的作业中,您cases := []struct之前[]这意味着您要声明此匿名类型的一部分。

接下来的一点点,称为静态启动。这是用于初始化集合类型的语法。这些嵌套位中的每一个如struct都是这些匿名结构之一的声明和启动,再次用花括号表示。在这种情况下,您分别将两个空字符串分配给{"", ""}in(如果不使用名称,则顺序与定义中的顺序相同)。外面的一对括号用于切片。如果您的切片是单词或字符串,那么您只需要将值放在那里,而不需要像want这样的额外嵌套级别。

myInts := []int{5,6,7}

答案 1 :(得分:1)

了解什么是结构。

然后在其中声明您的变量 你可以从一个函数中使用它。 例如:

package main

import (
    "fmt"

)

func main() {
    Get()

}

func Get(){
    out := new(Var)

    out.name = "james"

    fmt.Println(out.name)
}
type Var struct {
    name string
}

答案 2 :(得分:0)

您可以找到有关Golang Struc

的更多信息
// GO language program with an example of Struc Type

package main

import (
"fmt"
)

func main() {
    type Salary struct{
        Basic, HRA, TA float64      
    }

    type Employee struct{
        FirstName, LastName, Email string
        Age int
        MonthlySalary []Salary
    }

    e := Employee{
        FirstName: "Mark",
        LastName: "Jones",
        Email: "mark@gmail.com",
        Age: 25,
        MonthlySalary: []Salary{
            Salary{
                Basic:15000.00,
                HRA:5000.00,
                TA:2000.00,
            },
            Salary{
                Basic:16000.00,
                HRA:5000.00,
                TA:2100.00,
            },
            Salary{
                Basic:17000.00,
                HRA:5000.00,
                TA:2200.00,
            },
        },
    }
    fmt.Println(e.FirstName,e.LastName)
    fmt.Println(e.Age)
    fmt.Println(e.Email)
    fmt.Println(e.MonthlySalary[0])
    fmt.Println(e.MonthlySalary[1])
    fmt.Println(e.MonthlySalary[2])
}
相关问题