Last active
January 2, 2023 16:16
-
-
Save Elshaman/9a10a6b003c473a17f1ea227e6038a0e to your computer and use it in GitHub Desktop.
Golang: update attributes in struct using * operator
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
go build -o structura path/to/main.go |
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 main | |
import "fmt" | |
type person struct { | |
first string | |
last string | |
} | |
// fullName returns the full name of a person | |
func (p person) fullName() string { | |
return p.first + " " + p.last | |
} | |
//el objeto al cual se quiere atar el método se pasa por valor al método changeName | |
//para que sea pasado por referencia, se utiliza el operador * | |
func(p *person) changeName(first, last string){ | |
p.first = first | |
p.last = last | |
} | |
// define author and embed person | |
// | |
// override fullName method for author | |
// | |
func main() { | |
// initialize and print a person's full name | |
p := person{ | |
first: "Toni", | |
last: "Morrison", | |
} | |
fmt.Println(p.fullName()) | |
p.changeName("Tamara" , "Chaverra") | |
// initialize and print an author's full name | |
fmt.Println(p.fullName()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment