Skip to content

Instantly share code, notes, and snippets.

@mhewedy
Last active August 4, 2019 22:35
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 mhewedy/5a49a0af4f51733b72d6773fed2611c6 to your computer and use it in GitHub Desktop.
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)
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