Created
September 2, 2019 14:53
-
-
Save sammyne/d77be41112df33f53ccc40a20e5a605a 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 { | |
// [:i:i] to shrink the capacity | |
p.favoriteColors = append(p.favoriteColors[:i: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 G B]} | |
// {[R xx B]} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment