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
I thought I understood how iteration over an array works but apparently not in Golang
pointers - Range references instead values - Stack Overflow
loops - Is there a way to iterate over a range of integers? - Stack Overflow
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
From Go 1.22 (expected release February 2024), you will be able to write:
for i := range 10 {
fmt.Println(i+1)
}
(ranging over an integer in Go iterates from 0 to one less than that integer).
For versions of Go before 1.22, the idiomatic approach is to write a for loop like this.
for i := 1; i <= 10; i++ {
fmt.Println(i)
}
It was suggested by Mark Mishyn to use slice but there is no reason to create array with make and use in for returned slice of it when array created via literal can be used and it's shorter
for i := range [5]int{} {
fmt.Println(i)
}