Created
September 2, 2019 14:49
-
-
Save sammyne/e845c24ad89ef04fd2207cdc6196e29f to your computer and use it in GitHub Desktop.
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 { | |
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