如何构造一个切片结构的对象?

huangapple go评论93阅读模式
英文:

How do I construct an object of a slice struct?

问题

package main

import (
	"fmt"
)

type demo struct {
	Text string
	Type string
}

func main() {
	d := demo{
		Text: "Hello",
		Type: "string",
	}
	fmt.Println(d)
}

在这段代码中,我在声明demo结构体的对象时遇到了一个错误:

./prog.go:11:3: undefined: Text
./prog.go:11:9: cannot use "Hello" (untyped string constant) as struct{Text string; Type string} value in array or slice literal
./prog.go:12:3: undefined: Type
./prog.go:12:9: cannot use "string" (untyped string constant) as struct{Text string; Type string} value in array or slice literal

这是因为这不是一个普通的结构体声明,所以请告诉我如何构造demo结构体的对象?

英文:
package main

import (
	"fmt"
)
type demo []struct {
	Text string
	Type string
}
func main() {
	
	d := demo{
		Text: "Hello",
		Type: "string",
	}
	
}

In this code I'm getting an error while declaring the object of demo struct:

> ./prog.go:11:3: undefined: Text<br>
> ./prog.go:11:9: cannot use "Hello" (untyped string constant) as struct{Text string; Type string} value in array or slice literal<br>
./prog.go:12:3: undefined: Type<br>
./prog.go:12:9: cannot use "string" (untyped string constant) as struct{Text string; Type string} value in array or slice literal

it's obvious because it is not a normal struct declaration so please help me how can I construct object of demo struct?

答案1

得分: 1

由于您将demo声明为一个匿名结构体的切片,因此您必须使用demo{}来构造切片,并使用{Text: "Hello", Type: "string"}来构造项目。

func main() {
    d := demo{{
        Text: "Hello",
        Type: "Anon",
    }}

    fmt.Println(d)
    // [{Hello Anon}]
}

作为一个切片,您也可以使用make来创建它,但是添加项目需要复制匿名结构体的定义:

func main() {
    d1 := make(demo, 0)
    d1 = append(d1, struct {
        Text string
        Type string
    }{"Hello", "Append"})

    fmt.Println(d1)
    // [{Hello Append}]
}

然而,尽管它可以编译,但这是相当不常见的。只需定义命名的结构体类型,然后将d定义为这些结构体的切片。语法几乎相同,但更直观:

// 只需定义结构体类型
type demo struct {
    Text string
    Type string
}

func main() {
    d2 := []demo{{
        Text: "Hello",
        Type: "Slice",
    }}
    fmt.Println(d2)
    // [{Hello Slice}]
}

Playground: https://go.dev/play/p/4kSXqYKEhst

英文:

Since you declared demo as a slice of anonymous structs, you have to use demo{} to construct the slice and {Text: &quot;Hello&quot;, Type: &quot;string&quot;} to construct the item(s).

func main() {
	d := demo{{
		Text: &quot;Hello&quot;,
		Type: &quot;Anon&quot;,
	}}

	fmt.Println(d)
	// [{Hello Anon}]
}

Being a slice, you can also make it, but then appending items requires replicating the definition of the anonymous struct:

func main()
	d1 := make(demo, 0)
	d1 = append(d1, struct {
		Text string
		Type string
	}{&quot;Hello&quot;, &quot;Append&quot;})

	fmt.Println(d1)
	// [{Hello Append}]
}

However albeit it compiles, it is rather uncommon. Just define the named struct type, and then d as a slice of those. The syntax is almost the same, but straightforward:

// just defined struct type
type demo struct {
    Text string
    Type string
}

func main() {
    
    d2 := []demo{{
        Text: &quot;Hello&quot;,
        Type: &quot;Slice&quot;,
    }}
    fmt.Println(d2)
    // [{Hello Slice}]
}

Playground: https://go.dev/play/p/4kSXqYKEhst

huangapple
  • 本文由 发表于 2021年12月4日 22:44:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/70226630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定