Last active
September 6, 2016 15:35
-
-
Save namsral/c4ed7aa6650221146cd1faa8c5ac9748 to your computer and use it in GitHub Desktop.
Example of a getter and setter in a single method
This file contains 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 person_test | |
import ( | |
"fmt" | |
"strings" | |
) | |
// Person is a modern human with a name. | |
type Person struct { | |
name string | |
} | |
// Name is both a getter and setter to the name property. | |
func (p *Person) Name(value ...string) string { | |
if len(value) > 0 { | |
p.name = strings.Title(value[len(value)-1]) | |
} | |
return p.name | |
} | |
func ExampleGetSet() { | |
p := &Person{name: "marie"} | |
p.Name("anna") | |
name := p.Name() | |
fmt.Println(name) | |
// Output: Anna | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment