Last active
August 4, 2019 22:35
-
-
Save mhewedy/5a49a0af4f51733b72d6773fed2611c6 to your computer and use it in GitHub Desktop.
Go is not easy at all, it is not java or ruby or c#, it is a simpler c++ (nasty side effects of using append on slices)
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 user struct { | |
likes int | |
} | |
func main() { | |
users := make([]user, 3) | |
shareUser := &users[1] | |
doLike(shareUser) | |
fmt.Printf("User: at index 1 Likes: %v\n", users[1].likes) | |
users = append(users, user{}) | |
doLike(shareUser) | |
fmt.Printf("User: at index 1 Likes: %v\n", users[1].likes) // actual 1, expected 2 | |
} | |
func doLike(user *user) { | |
user.likes++ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment