Skip to content

Instantly share code, notes, and snippets.

@stevenvo
Last active July 27, 2020 00:32
Show Gist options
  • Save stevenvo/5d278a0df2c1a70e07a38240df555040 to your computer and use it in GitHub Desktop.
Save stevenvo/5d278a0df2c1a70e07a38240df555040 to your computer and use it in GitHub Desktop.
Struct #go #struct
// Definition
type identifier struct {
field1 type1
field2 type2
...
}
// Example struct defintion for use below
type struct1 struct { // struct definition
i1 int
f1 float32
str string
}
// Initialization of Struct
// there are MANY ways to initialize a struct
// variable, depending on how do you want to obtain
// the variable, in direct reference, or ptr address reference
// GROUP 1: DIRECT value-type, using `var` or `:=` (assignemnt)
// keyword for value-type initilize, when passing
// the struct var to another func that accepts struct pointer,
// you need to pass with `&`, for example: func1(&t)
// 1. `var` method
var t struct1
t.i1 = 10
t.f1 = 15.5
t.str = "Chris"
// 2. `:=` method
t := struct1{ // note: the `&` before struct type is omitted
i1: 10,
f1: 15.5,
str: "Chris",
}
// GROUP 2: PTR/Address reference type, using different combination of
// `var`, `new`, `&`, or `:=` (assignemnt) keyword
// for PTR reference initilize, when passing the struct
// var to another func that accepts struct pointer,
// you DON'T need to pass with `&`, for example: func1(t)
// 3. `var` & `new` method
var t *struct1 //ptr type variable
t = new(struct1) //note: `new` will always return a ptr
t.i1 = 10
t.f1 = 15.5
t.str = "Chris"
// 4. `new` and `:=` method
t := new(struct1) //assignment will infer the ptr type for t
t.i1 = 10
t.f1 = 15.5
t.str = "Chris"
// 5. `&` and `:=` method
t := &struct1{} // declaration of fields AFTER initialization
t.i1 = 10
t.f1 = 15.5
t.str = "Chris"
// other variation of this method
// declaration of fields DURING initialization, need correct order
t := &struct1{10, 15.5, "Chris"} // This is the MOST popular
// declaration of fields DURING initialization with name field, no need correct order
t := &struct1{i1: 10, f1: 15.5, str: "Chris"}
// declaration of fields DURING initialization with name field, multiline format
t := &struct1{
i1: 10,
f1: 15.5,
str: "Chris", //note that it MUST have `,` comma here to avoid ending the statment
}
// `new` keyword: it's also noted to mention that "new"
// is LESS popular with regard to structs (v/s other methods),
// `new` always create an instance of the type you want and
// instead of returing the plain declaration of the type,
// it references it and return the memory address (&) of
// that type in the program process heap
/** ============================================ **/
/** ============================================ **/
/** ============================================ **/
/** ============================================ **/
// Anynomous fields
// Declaration of anynomous field in struct only needs TYPE
type outerS struct {
b int
c float32
int // anonymous field
}
outer := new(outerS)
outer.b = 6
outer.c = 7.5
outer.int = 60 // there is one drawback: only ONE anonymous field
// of int type will be allowed
// Anynomous + Composite behavior
// A struct can "absorb" the fields of another struct by embedding
// and declaring the sub struct as anonymouse fields
// sub struct
type innerS struct {
in1 int
in2 int
}
// main struct
type outerS struct {
b int
c float32
innerS // struct type as anonymous field
}
outer := new(outerS)
outer.b = 6
outer.c = 7.5
// the fields of sub struct can be directly accessed from the main struct
outer.in1 = 5
outer.in2 = 10
// which is equivalent to
outer.innerS.in1 = 5
outer.innerS.in2 = 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment