我正在努力解决结构(GO语言)中分片的启动问题。这可能很简单,但我还是无法解决。我得到以下错误信息
./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 :=
我相信 s
应该被声明为结构的一部分,所以我不知道为什么会出现这个错误。有人有什么建议吗?
package main
import "fmt"
type node struct {
value int
}
type graph struct {
nodes , edges int
var 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
}
你有几个错误。
g.s
已经被类型定义为 graph
当 g
属于 graph
. 所以这不是一个 "新变量"var
在类型声明中g
中已经声明了(作为返回类型)。graphCreate
功能这是一个编译代码。
package main
import "fmt"
type node struct {
value int
}
type graph struct {
nodes, edges int
s []int // <= there was var here
}
func main() {
graphCreate() // <= g wasn't used
}
func input(tname string) (number int) {
fmt.Println("input a number of " + tname)
fmt.Scan(&number)
return
}
func graphCreate() (g graph) { // <= g is declared here
g = graph{nodes:input("nodes"), edges:input("edges")} // <= name the fields
g.s = make([]int, 100) // <= g.s is already a known name
return
}