Complex Structs

Now we combine structs types to a complex collection.

The strength level should now be separate values. We define constants with the iota keyword, which just counts up:

type Strength int

const (
    Weak Strength = iota
    Middle
    Strong
    Galactus
)

type Supe struct {
    Name string
    Powerlevel Strength
}

Now Supe has a simple Name which has the type string and a Powerlevel as new type.

So we can work with the new types:

package main

import (
        "fmt"
)

type Strength int

const (
    Weak Strength = iota
    Middle
    Strong
    Galactus
)

type Supe struct {
    Name string
    Powerlevel Strength
}

func main() {
        var shazam Supe
        shazam.Name = "shazam"
        shazam.Powerlevel = Strong
        fmt.Println(shazam)
}

This gives with go run main.go or in the playground:

{shazam 2}

Now as we know almost all datatypes we go on to the swiss army knife, the incredible interface.

Source

See the full source on github.

Sources