Skip to content

Instantly share code, notes, and snippets.

@Elshaman
Last active January 2, 2023 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elshaman/9a10a6b003c473a17f1ea227e6038a0e to your computer and use it in GitHub Desktop.
Save Elshaman/9a10a6b003c473a17f1ea227e6038a0e to your computer and use it in GitHub Desktop.
Golang: update attributes in struct using * operator
go build -o structura path/to/main.go
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