📝 Useful code snippets for structs

Creation

type struct1 struct {
  field1 type1
  field2 type2
  ...
}

ms := new(struct1)

Initialization

ms := &struct1{10, 15.5, "Chris"}

Capitalize the first letter of the struct name to make it visible outside its package. Often, it is better to define a factory function for the struct and force using that.

ms := Newstruct1{10, 15.5, "Chris"}

func Newstruct1(n int, f float32, name string) *struct1 {
  return &struct1{n, f, name}
}

Get hands-on with 1200+ tech skills courses.