Skip to content

Instantly share code, notes, and snippets.

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 sammyne/e845c24ad89ef04fd2207cdc6196e29f to your computer and use it in GitHub Desktop.
Save sammyne/e845c24ad89ef04fd2207cdc6196e29f to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Person struct {
favoriteColors []string
}
func (p Person) WithFavoriteColorAt(i int, favoriteColor string) Person {
p.favoriteColors = append(p.favoriteColors[:i],
append([]string{favoriteColor}, p.favoriteColors[i+1:]...)...)
return p
}
func main() {
p := Person{
favoriteColors: []string{"R", "G", "B"},
}
x := p.WithFavoriteColorAt(1, "xx")
fmt.Println(p)
fmt.Println(x)
// Output:
// {[R xx B]}
// {[R xx B]}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment