英文:
Go: declaring a slice inside a struct?
问题
你的代码有几个语法错误。首先,在house结构体中,你不能在类型声明中使用:=
运算符。你应该在结构体外部声明并初始化变量。其次,在house结构体中,你不能在类型声明中使用:=
运算符,你应该在函数体内部进行初始化。最后,你的main函数是空的,你需要在其中添加一些代码。
以下是修改后的代码:
type room struct {
width float32
length float32
}
type house struct {
s []string
name string
roomSzSlice []room
}
func main() {
h := house{
s: make([]string, 3),
name: "",
roomSzSlice: make([]room, 3),
}
// 在这里添加你的代码
}
希望对你有帮助!
英文:
I have the following code:
type room struct {
width float32
length float32
}
type house struct{
s := make([]string, 3)
name string
roomSzSlice := make([]room, 3)
}
func main() {
}
And when i try to build and run it i get the following errors:
c:\go\src\test\main.go:10: syntax error: unexpected :=
c:\go\src\test\main.go:11: non-declaration statement outside function body
c:\go\src\test\main.go:12: non-declaration statement outside function body
c:\go\src\test\main.go:13: syntax error: unexpected }
What did i do wrong?
Thanks!
答案1
得分: 7
你可以在结构体声明中声明一个切片,但是不能初始化它。你需要通过其他方式来进行初始化。
请注意,小写标识符是不可导出的,因此无法访问。
type House struct {
s []string
name string
rooms []room
}
// 所以你需要访问器或者getter,例如:
func (h *House) Rooms() []room {
return h.rooms
}
// 由于你的字段是不可访问的,你需要创建一个"构造函数"
func NewHouse(name string) *House {
return &House{
name: name,
s: make([]string, 3),
rooms: make([]room, 3),
}
}
请参考上述内容作为一个可运行的示例:Go Playground上的示例
编辑:
根据评论中的要求,可以部分初始化结构体,只需简单地更改如下代码:
func NewHouse(name string) *House {
return &House{
name: name,
}
}
请参考上述内容作为一个可运行的示例:Go Playground上的示例
英文:
You can declare a slice in a struct declaration, but you can not initialize it. You have to do this by different means.
// Keep in mind that lowercase identifiers are
// not exported and hence are inaccessible
type House struct {
s []string
name string
rooms []room
}
// So you need accessors or getters as below, for example
func(h *House) Rooms()[]room{
return h.rooms
}
// Since your fields are inaccessible,
// you need to create a "constructor"
func NewHouse(name string) *House{
return &House{
name: name,
s: make([]string, 3),
rooms: make([]room, 3),
}
}
Please see the above as a <kbd>runnable example on Go Playground</kbd>
EDIT
To Initialize the struct partially, as requested in the comments, one can simply change
func NewHouse(name string) *House{
return &House{
name: name,
}
}
Please see the above as a <kbd>runnable example on Go Playground</kbd>, again
答案2
得分: 7
首先,你不能在结构体内部进行赋值/初始化操作。:= 运算符用于声明和赋值。不过,你可以通过简单的方式实现相同的结果。
下面是一个简单的示例,大致实现了你想要的功能:
type house struct {
s []string
}
func main() {
h := house{}
a := make([]string, 3)
h.s = a
}
我从未以这种方式编写过代码,但如果它能满足你的需求...它无论如何都可以编译通过。
英文:
First off you cannot assign/initialize inside the struct. The := operator declares and assigns. You can however, achieve the same result simply.
Here is a simple, trivial example that would do roughly what you're trying:
type house struct {
s []string
}
func main() {
h := house{}
a := make([]string, 3)
h.s = a
}
I've never written one that way, but if it servers your purpose... it compiles anyway.
答案3
得分: -1
你可以参考这个链接:https://golangbyexample.com/struct-slice-field-go/
type student struct {
name string
rollNo int
city string
}
type class struct {
className string
students []student
}
goerge := student{"Goerge", 35, "Newyork"}
john := student{"Goerge", 25, "London"}
students := []student{goerge, john}
class := class{"firstA", []student{goerge, john}}
英文:
You can also refer this: https://golangbyexample.com/struct-slice-field-go/
type student struct {
name string
rollNo int
city string
}
type class struct {
className string
students []student
}
goerge := student{"Goerge", 35, "Newyork"}
john := student{"Goerge", 25, "London"}
students := []student{goerge, john}
class := class{"firstA", []student{goerge, john}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论