首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >围棋中的结构体

围棋中的结构体
EN

Stack Overflow用户
提问于 2018-11-25 18:51:28
回答 1查看 9.6K关注 0票数 2

我想列举一下我的围棋程序中的行星。每颗行星都有一个共同的名字(例如:“金星”)和天文单位距离太阳的距离(例如: 0.722)。

所以我写了这段代码:

代码语言:javascript
复制
type planet struct {
    commonName string
    distanceFromTheSunInAU float64
}

const(
    venus planet = planet{"Venus", 0.387}      // This is line 11
    mercury planet = planet{"Mercury", 0.722}
    earth planet = planet{"Eath", 1.0}
    mars planet = planet{"Mars", 1.52}
    ...
)

但是Go不允许我编译它,并给了我这个错误:

代码语言:javascript
复制
# command-line-arguments
./Planets.go:11: const initializer planet literal is not a constant
./Planets.go:12: const initializer planet literal is not a constant
./Planets.go:13: const initializer planet literal is not a constant
./Planets.go:14: const initializer planet literal is not a constant

你知道我能做什么吗?谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-25 19:43:20

Go不支持枚举。您应该将枚举字段定义为var,或者为了确保不可变,可以使用返回常量结果的函数。

例如:

代码语言:javascript
复制
type myStruct { ID int }

func EnumValue1() myStruct { 
    return myStruct { 1 } 
}

func EnumValue2() myStruct { 
    return myStruct { 2 } 
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53470766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档