create a struct with a field of type int in go

To create a struct with a field of type int in Go, we can define a new type with the type keyword followed by the struct name and its fields.

main.go
type MyStruct struct {
    myIntField int
}
44 chars
4 lines

In the example above, we defined a new struct named MyStruct with a single field called myIntField of type int. Now we can create a new instance of the MyStruct struct and set its myIntField field:

main.go
myStruct := MyStruct{myIntField: 42}
37 chars
2 lines

In this second example, we created a new instance of MyStruct and set its myIntField field to the value 42.

related categories

gistlibby LogSnag