I'm new to go so forgive me if this is a trivial question. I want to iterate over a slice of posts and increment the value of Views
of each post:
func incrementViews(posts []model.Post) []model.Post {
for _, v := range posts {
v.Views++
fmt.Println(v.Views) //Views incremented by 1
}
return posts
}
incrementViews(posts) //Views not changed
The printed values are changed but when I call incrementViews(posts)
the returned values are unchanged.
I tried to solve this by using *
of &
but could not manage to do so perhaps because I come from Python background and have lose grasp of moving around variables by pointers and values.