init function for structs

Go doesn’t have implicit constructors. You would likely write something like this.

package main

import "fmt"

type Console struct {
    X int
    Y int
}

func NewConsole() *Console {
    return &Console{X: 5}
}

var console Console = *NewConsole()

func main() {
    fmt.Println(console)
}

Output:

{5 0}

Leave a Comment