结构体中切片的初始化

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

init of slice in struct

问题

我正在努力解决在结构体中初始化切片的问题(GO语言)。这可能很简单,但我仍然无法解决它。我得到以下错误:

./prog.go:11:1: 语法错误:意外的 var,期望字段名或嵌入类型
./prog.go:25:2: := 左侧没有新变量
./prog.go:26:2: := 左侧的非名称 g.s

我相信 s 应该作为结构体的一部分声明,所以我想知道为什么会出现这个错误。有人有什么建议吗?

package main

import "fmt"

type node struct {
	value int
}

type graph struct {
	nodes, edges int
	s            []int
}

func main() {
	g := graphCreate()
}

func input(tname string) (number int) {
	fmt.Println("input a number of " + tname)
	fmt.Scan(&number)
	return
}

func graphCreate() (g graph) {
	g := graph{input("nodes"), input("edges")}
	g.s = make([]int, 100)
	return
}
英文:

I am struggling with the initiation of a slice in a struct (GO-language). This may be easy, but still I can not solve it. I get below error

./prog.go:11:1: syntax error: unexpected var, expecting field name or embedded type
./prog.go:25:2: no new variables on left side of :=
./prog.go:26:2: non-name g.s on left side of :=

I believe that s should be declared as part of the struct, so I wonder why I get that error. Someone got some advice?

package main

import "fmt"

type node struct {
	value int
}

type graph struct {
	nodes, edges int
	s            []int
}

func main() {
	g := graphCreate()
}

func input(tname string) (number int) {
	fmt.Println("input a number of " + tname)
	fmt.Scan(&number)
	return
}

func graphCreate() (g graph) {
	g := graph{input("nodes"), input("edges")}
	g.s = make([]int, 100)
	return
}

答案1

得分: 12

你有几个错误:

  • g的类型为graph时,g.s已经被类型graph定义,所以它不是一个"新变量"
  • 你不能在类型声明中使用var
  • 你已经在graphCreate函数中声明了g(作为返回类型)
  • 当你写一个字面结构时,你必须传递所有字段值或命名它们
  • 你必须使用你声明的变量

这是一个可以编译的代码:

package main

import "fmt"

type node struct {
	value int
}

type graph struct {
	nodes, edges int
	s            []int // <= 这里原本有一个var
}

func main() {
	graphCreate() // <= 没有使用g
}

func input(tname string) (number int) {
	fmt.Println("input a number of " + tname)
	fmt.Scan(&number)
	return
}

func graphCreate() (g graph) { // <= 这里声明了g
	g = graph{nodes: input("nodes"), edges: input("edges")} // <= 命名字段
	g.s = make([]int, 100) // <= g.s已经是一个已知的名称
	return
}
英文:

You have a few errors :

  • g.s is already defined by the type graph when g is of type graph. So it's not a "new variable"
  • you can't use var inside a type declaration
  • you have g already declared (as a return type) in your graphCreate function
  • when you write a literal struct, you must pass none or all the field values or name them
  • you must use the variables you declare

here's a compiling code :

package main

import &quot;fmt&quot;

type node struct {
	value int
}

type graph struct {
	nodes, edges int
	s            []int // &lt;= there was var here
}

func main() {
	graphCreate() // &lt;= g wasn&#39;t used
}

func input(tname string) (number int) {
	fmt.Println(&quot;input a number of &quot; + tname)
	fmt.Scan(&amp;number)
	return
}

func graphCreate() (g graph) { // &lt;= g is declared here
	g = graph{nodes:input(&quot;nodes&quot;), edges:input(&quot;edges&quot;)} // &lt;= name the fields
	g.s = make([]int, 100) // &lt;= g.s is already a known name
	return
}

huangapple
  • 本文由 发表于 2013年9月16日 20:15:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/18827800.html
匿名

发表评论

匿名网友

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

确定