Created
April 5, 2018 12:52
-
-
Save afreeland/ea7acc7e47ee28b632b3eb13f2ce3d7d to your computer and use it in GitHub Desktop.
Go: constructors (how to)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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