简体   繁体   English

Go 语言中的结构

[英]Structs in GoLang

I am just starting with GoLang, and I am looking at one of their tutorials ( https://golang.org/doc/code.html ).我刚开始使用 GoLang,我正在看他们的教程之一 ( https://golang.org/doc/code.html )。

In one of their examples, they set a variable to a struct, but I am so confused as to how they are accessing elements of the struct in the for loop below?在他们的一个示例中,他们将变量设置为结构,但我很困惑他们如何在下面的 for 循环中访问结构的元素? Any chance someone can clarify?有人可以澄清的机会吗? Thanks alot!非常感谢!

Code:代码:

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)
        }
    }
}

Below is the code with some comments to help clarify each statements role in this.下面是带有一些注释的代码,以帮助阐明每个语句在其中的作用。

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
        }
    }
}

Let me know if that helps.让我知道是否有帮助。 I can try giving more of a summary in spoken language or add more details if some of the statements don't make sense still.如果某些陈述仍然没有意义,我可以尝试提供更多的口头摘要或添加更多细节。 Also, fyi if you're not familiar range 'ranges' over a collection, returning k, v where k is the index or key and v the value.另外,如果您不熟悉集合的范围“范围”,请参考,返回k, v ,其中k是索引或键, v是值。

EDIT: details on the declaration/initilization of cases编辑:有关cases声明/启动的详细信息

    cases := []struct {
        in, want string
    }

This bit inside the first pair of curly braces is the definition of a struct.第一对大括号内的这一点是结构的定义。 This is an anonymous type, a normal declaration would look like this;这是一个匿名类型,一个普通的声明看起来像这样;

    type case struct {
        in string
        want string
    }

If you had something like this then there would be a type called case in the scope of this package (not exported, if you wanted to make it 'public' so it would need to be type Case instead).如果你有这样的东西,那么在这个包的范围内会有一个名为case的类型(不导出,如果你想让它“公开”,那么它需要改为type Case )。 Instead the examples struct is anonymous.相反,示例结构是匿名的。 It works the same as normal type, however as a developer, you will have no way to reference that type so you can only practically work with the collection initialized here.它与普通类型一样工作,但是作为开发人员,您将无法引用该类型,因此您实际上只能使用此处初始化的集合。 Internally this type is the same as any other struct with 2 unexported strings for fields.在内部,此类型与任何其他具有 2 个未导出字段字符串的结构相同。 The fields are named in and want .这些字段命名为inwant Notice that in the assignment here cases:= []struct you have [] before struct this means you're declaring a slice of this anonymous type.请注意,在此处的赋值中, cases:= []structstruct之前有[]这意味着您要声明此匿名类型的一部分。

This next little bit, is called static initialization.接下来的一点,称为静态初始化。 This is a syntax for initializing collections as types.这是将集合初始化为类型的语法。 Each of these nested bits like {"", ""} is the declaration and initilization of one of these anonymous structs, denoted again by the curly braces.这些嵌套位(如{"", ""} )中的每一个都是这些匿名结构之一的声明和初始化,再次由大括号表示。 In this case you're assigning two empty strings to in and want respectively (if you don't use names, the order is the same as in the definition).在这种情况下,您分别将两个空字符串分配给inwant (如果您不使用名称,则顺序与定义中的顺序相同)。 The outer pair of braces is for the slice.外面一对大括号用于切片。 If your slice were of say int's or string's, then you would just have the values right there without the extra level of nesting like myInts:= []int{5,6,7} .如果您的切片是 int 或 string 的切片,那么您将只拥有这些值,而无需像myInts:= []int{5,6,7}这样的额外嵌套级别。

    {
        {"Hello, world", "dlrow ,olleH"},
        {"Hello, 世界", "界世 ,olleH"},
        {"", ""},
    }

Go root of what is a struct.去根什么是结构。

you declare your variables in it so then you can use it from a function.你在其中声明你的变量,这样你就可以从函数中使用它。 Example:例子:

package main

import (
    "fmt"

)

func main() {
    Get()

}

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

    out.name = "james"

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM