No need for a public function

This commit is contained in:
Mark McGranaghan 2020-04-10 17:50:26 -07:00
parent a1fd3bf037
commit 0b7926ac8d

View File

@ -12,8 +12,8 @@ type person struct {
age int
}
// NewPerson constructs a new person struct with the given name
func NewPerson(name string) *person {
// `newPerson` constructs a new person struct with the given name.
func newPerson(name string) *person {
// You can safely return a pointer to local variable
// as a local variable will survive the scope of the function.
p := person{name: name}
@ -36,7 +36,7 @@ func main() {
fmt.Println(&person{name: "Ann", age: 40})
// It's idiomatic to encapsulate new struct creation in constructor functions
fmt.Println(NewPerson("Jon"))
fmt.Println(newPerson("Jon"))
// Access struct fields with a dot.
s := person{name: "Sean", age: 50}