go make struct code example

Example 1: go create new instance of struct

type Person struct {
	Name string
}
 
func main() {
	var me Person
	me.name = "mattalui"
}

Example 2: init struct go

type Student struct {
    Name string
    Age  int
}

var a Student    // a == Student{"", 0}
a.Name = "Alice" // a == Student{"Alice", 0}

Example 3: inizialiyze struct di struct golang

package main

import "fmt"

func main() {
    result := Result{
        Name: "I am Groot",
        Objects: []MyStruct{
            {
                MyField: 1,
            },
            {
                MyField: 2,
            },
            {
                MyField: 3,
            },
        },
    }

    fmt.Println(result)

}

type MyStruct struct {
    MyField int
}

type Result struct {
    Name    string
    Objects []MyStruct
}

Tags:

Go Example