January 2018
Intermediate to advanced
340 pages
8h 6m
English
There are no constructors in Go, but there are New() functions that act like factories initializing an object. You simply have to create a function named New() that returns your data type. Here is an example:
package mainimport "fmt"type Person struct { Name string}func NewPerson() Person { return Person{ Name: "Anonymous", }}func main() { p := NewPerson() fmt.Println(p)}
There are no deconstructors in Go, since everything is garbage collected and you do not manually destroy objects. Defer is the closest you can get by deferring a function call to perform some cleanup when the current function ends.
Read now
Unlock full access