Skip to content

Instantly share code, notes, and snippets.

@afreeland
Created April 5, 2018 12:52
Show Gist options
  • Save afreeland/ea7acc7e47ee28b632b3eb13f2ce3d7d to your computer and use it in GitHub Desktop.
Save afreeland/ea7acc7e47ee28b632b3eb13f2ce3d7d to your computer and use it in GitHub Desktop.
Go: constructors (how to)
package main
import (
"fmt"
)
func main() {
foo := newMyStruct()
foo.myMap["bar"] = "baz"
fmt.Println(foo)
}
type myStruct struct {
myMap map[string]string
}
// Create a constructor function that allows us to initialize values (such as map)
func newMyStruct() *myStruct {
result := myStruct{}
result.myMap = map[string]string{}
return &result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment