No need for a public function
This commit is contained in:
parent
a1fd3bf037
commit
0b7926ac8d
@ -12,8 +12,8 @@ type person struct {
|
|||||||
age int
|
age int
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPerson constructs a new person struct with the given name
|
// `newPerson` constructs a new person struct with the given name.
|
||||||
func NewPerson(name string) *person {
|
func newPerson(name string) *person {
|
||||||
// You can safely return a pointer to local variable
|
// You can safely return a pointer to local variable
|
||||||
// as a local variable will survive the scope of the function.
|
// as a local variable will survive the scope of the function.
|
||||||
p := person{name: name}
|
p := person{name: name}
|
||||||
@ -36,7 +36,7 @@ func main() {
|
|||||||
fmt.Println(&person{name: "Ann", age: 40})
|
fmt.Println(&person{name: "Ann", age: 40})
|
||||||
|
|
||||||
// It's idiomatic to encapsulate new struct creation in constructor functions
|
// 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.
|
// Access struct fields with a dot.
|
||||||
s := person{name: "Sean", age: 50}
|
s := person{name: "Sean", age: 50}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user