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?
2 ways to create and initialize a new structThe new keyword can be used to create a new struct. It returns a pointer to the newly created struct. You can also create and initialize a struct with a struct literal. An element list that contains keys does not need to have an element for each struct field.
In Go language, arrays are mutable, so that you can use array[index] syntax to the left-hand side of the assignment to set the elements of the array at the given index. You can access the elements of the array by using the index value or by using for loop. In Go language, the array type is one-dimensional.
It looks like you are trying to use (almost) straight up C code here. Go has a few differences.
const
. The term const
has a different meaning in Go, as it does in C. The list should be defined as var
instead.basenameOpts
as opposed to basename_opts
.char
type in Go. You probably want byte
(or rune
if you intend to allow unicode codepoints).var x = foo
.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.
Adding this just as an addition to @jimt's excellent answer:
one common way to define it all at initialization time is using an anonymous struct:
var opts = []struct { shortnm byte longnm, help string needArg bool }{ {'a', "multiple", "Usage for a", false}, { shortnm: 'b', longnm: "b-option", needArg: false, help: "Usage for b", }, }
This is commonly used for testing as well to define few test cases and loop through them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With