Something like that, than marshal to json
package main
import "fmt"
var StringsArray = []string{"Person1", "Person2", "Person3",
"Person4", "Person1", "Person2"}
type MyStruct struct {
Name string
Count int
}
func main() {
mySlice := []MyStruct{}
for i, name := range StringsArray {
fmt.Println(name, i)
mySlice = append(
mySlice,
MyStruct{
Name: name,
Count: i,
})
}
fmt.Println(mySlice)
}
you can try it here - go playground
Answer from d1nch8g on Stack Overflowgo - Creating an Array of Objects in Range Loop Golang - Stack Overflow
pointers - Range references instead values - Stack Overflow
Golang how to range in pointer array - Stack Overflow
How does for and range work together in golang?
Videos
Something like that, than marshal to json
package main
import "fmt"
var StringsArray = []string{"Person1", "Person2", "Person3",
"Person4", "Person1", "Person2"}
type MyStruct struct {
Name string
Count int
}
func main() {
mySlice := []MyStruct{}
for i, name := range StringsArray {
fmt.Println(name, i)
mySlice = append(
mySlice,
MyStruct{
Name: name,
Count: i,
})
}
fmt.Println(mySlice)
}
you can try it here - go playground
The code mentioned above will have compilation issues. However based on the input-output mentioned, the following modification of your code should give you the desired results
package main
import "fmt"
var StringsArray = []string{"Person1", "Person2", "Person3", "Person4", "Person1", "Person2"}
type MyStruct struct {
Name string
Count int
}
func main() {
countArray := []MyStruct{}
Count := make(map[string]int)
for _, name := range StringsArray {
Count[name] += 1
}
for name, cnt := range Count {
countArray = append(countArray, MyStruct{Name: name, Count: cnt})
}
fmt.Println(countArray)
}
Playground Link - https://play.golang.org/p/tg4s30w98db
The short & direct answer: no, use the array index instead of the value
So the above code becomes:
package main
import "fmt"
type MyType struct {
field string
}
func main() {
var array [10]MyType
for idx, _ := range array {
array[idx].field = "foo"
}
for _, e := range array {
fmt.Println(e.field)
fmt.Println("--")
}
}
To combine @Dave C and @Sam Toliman's comments
package main
import "fmt"
type MyType struct {
field string
}
func main() {
var array [10]MyType
for idx := range array {
e := &array[idx]
e.field = "foo"
}
for _, e := range array {
fmt.Println(e.field)
fmt.Println("--")
}
}
https://play.golang.org/p/knKfziB1nce
Use the range keyword. https://github.com/golang/go/wiki/Range
for i, person := range *manager.allPerson {
fmt.Println(person.name)
}
Note that the compiler will be angry if you don't use the index var i. If you don't intend to use it, replace with _.
To further explain, your original error was due to the fact that you correctly dereferenced your *[]Person the first time, but not inside the for loop.
fmt.Println(manager.allPersons[:ii].name) // wrong
fmt.Println((*manager.allPersons)[:ii].name) // right
Also, given that your slice contains Person struct values, it will have to copy it if you use the optional second value in the range expression. Thus, it would be more efficient to keep using just the indexes.
As a suggestion, don't use a *[]Person, use []*Person, which is probably what you intended to use anyway. A slice is already a pointer value. Using []*Person you don't have to fear a copy by the range expression because it is simply a pointer to a Person instead of the entire struct.
In fact, unless you either change the type to []*Person or index into the slice you won't be able to have changes reflected in the slice because a struct value will be a copy in the range loop. See https://github.com/golang/go/wiki/Range#gotchas but be aware that if the value is a pointer value, this isn't an issue.
I think you might be mixed up by the pointers. You don't need to dereference your pointer anywhere. Something like this should work:
for i := 0; i < len(manager); i++ {
fmt.Println(manager[i].Name)
}
GoPlay here: https://play.golang.org/p/P8wAp4wIGs