douhuo0884 2014-10-02 10:59
浏览 94

Go中的结构初始化数组

I'm newbie in Go. This issue is driving me nuts. How do you init array of structs in Go ?

type opt struct {
    shortnm      char
    longnm, help string
    needArg      bool
}

const basename_opts []opt { 
        opt {
            shortnm: 'a', 
            longnm: "multiple", 
            needArg: false, 
            help: "Usage for a"}
        },
        opt {
            shortnm: 'b', 
            longnm: "b-option", 
            needArg: false, 
            help: "Usage for b"}
    }

The compiler said it expecting ';' after []opt.

Where should I put the braces '{' to init my array of struct ?

Thank you

  • 写回答

2条回答 默认 最新

  • dongxiong1941 2014-10-02 11:20
    关注

    It looks like you are trying to use (almost) straight up C code here. Go has a few differences.

    • First off, you can't initialize arrays and slices as const. The term const has a different meaning in Go, as it does in C. The list should be defined as var instead.
    • Secondly, as a style rule, Go prefers basenameOpts as opposed to basename_opts.
    • There is no char type in Go. You probably want byte (or rune if you intend to allow unicode codepoints).
    • The declaration of the list must have the assignment operator in this case. E.g.: var x = foo.
    • Go's parser requires that each element in a list declaration ends with a comma. This includes the last element. The reason for this is because Go automatically inserts semi-colons where needed. And this requires somewhat stricter syntax in order to work.

    For example:

    type opt struct {
        shortnm      byte
        longnm, help string
        needArg      bool
    }
    
    var basenameOpts = []opt { 
        opt {
            shortnm: 'a', 
            longnm: "multiple", 
            needArg: false, 
            help: "Usage for a",
        },
        opt {
            shortnm: 'b', 
            longnm: "b-option", 
            needArg: false, 
            help: "Usage for b",
        },
    }
    

    An alternative is to declare the list with its type and then use an init function to fill it up. This is mostly useful if you intend to use values returned by functions in the data structure. init functions are run when the program is being initialized and are guaranteed to finish before main is executed. You can have multiple init functions in a package, or even in the same source file.

    type opt struct {
        shortnm      byte
        longnm, help string
        needArg      bool
    }
    
    var basenameOpts []opt
    
    func init() { 
        basenameOpts = []opt{
            opt {
                shortnm: 'a', 
                longnm: "multiple", 
                needArg: false, 
                help: "Usage for a",
            },
            opt {
                shortnm: 'b', 
                longnm: "b-option", 
                needArg: false, 
               help: "Usage for b",
            },
        )
    }
    

    Since you are new to Go, I strongly recommend reading through the language specification. It is pretty short and very clearly written. It will clear a lot of these little idiosyncrasies up for you.

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图