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 Overflow
๐ŸŒ
Go by Example
gobyexample.com โ€บ range
Go by Example: Range
range iterates over elements in a variety of data structures. Letโ€™s see how to use range with some of the data structures weโ€™ve already learned ยท Here we use range to sum the numbers in a slice. Arrays work like this too
Discussions

go - Creating an Array of Objects in Range Loop Golang - Stack Overflow
I am a newbie in Golang and trying out things. I have two requirements, Print the Number of Occurrences of the Values in an Array - This is done. Creating a Key-Value of the Key and the Value in an Array - Still working on it. Let me share, what I have I done till now. I have array of Strings ยท var StringsArray :=["Person1","Person2","Person3","Person4","Person1","Person2"] ... for _, name := range ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
pointers - Range references instead values - Stack Overflow
I saw that range returns the key and the "copy" of the value. Is there a way for that range to return the address of the item? Example package main import "fmt" type MyType st... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Golang how to range in pointer array - Stack Overflow
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, ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How does for and range work together in golang?
Not always, but true for slices/arrays. ... Yes, youโ€™ve got to keep an eye on what youโ€™re iterating over, as the returns change significantly! ... I've been learning Go these last few weeks and I remember that range returns an index of where you are in the array/slice and the item in the array/slice ... Beginner to GoLang... More on reddit.com
๐ŸŒ r/golang
16
0
December 5, 2025
๐ŸŒ
YourBasic
yourbasic.org โ€บ golang โ€บ for-loop-range-array-slice-map-channel
4 basic range loop (for-each) patterns ยท YourBasic Go
yourbasic.org/golang ยท Basic for-each loop (slice or array) String iteration: runes or bytes ยท Map iteration: keys and values ยท Channel iteration ยท Gotchas ยท a := []string{"Foo", "Bar"} for i, s := range a { fmt.Println(i, s) } 0 Foo 1 Bar ยท The range expression, a, is evaluated once before beginning the loop.
๐ŸŒ
Programiz
programiz.com โ€บ golang โ€บ range
Go range (With Examples)
We can use the for range loop to access the individual index and element of an array. For example,
๐ŸŒ
VictoriaMetrics
victoriametrics.com โ€บ blog โ€บ how go arrays work and get tricky with for-range
How Go Arrays Work and Get Tricky with For-Range
August 2, 2024 - That means it works like a pass-by-value case. If our array is much bigger than just several elements, making a copy like this will be inefficient and the Go team has optimized this for us by allowing for-range with a pointer to the array.
๐ŸŒ
Go
go.dev โ€บ wiki โ€บ Range
Go Wiki: Range Clauses - The Go Programming Language
A range clause provides a way to iterate over an array, slice, string, map, or channel. for k, v := range myMap { log.Printf("key=%v, value=%v", k, v) } for v := range myChannel { log.Printf("value=%v", v) } for i, v := range myArray { log.Printf("array value at [%d]=%v", i, v) }
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ golang โ€บ range loop in golang
Range Loop in Golang - Scaler Topics
May 4, 2023 - A for-range loop in Golang is used for iterating over elements in various data structures like an array, slice, map, or even a string, etc.
Find elsewhere
๐ŸŒ
PixelsTech
pixelstech.net โ€บ home โ€บ articles โ€บ some tricks and tips for using for range in golang
Some tricks and tips for using for range in GoLang | PixelsTech
August 3, 2020 - // take address of the original array and loop for i, n := range &arr // create a slice based on original array for i, n := range arr[:] var arr = [102400]int{1, 1, 1} for i, _ := range &arr { arr[i] = 0 } The answer is yes as the GoLang has ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ go โ€บ go_range.htm
Go - Range
package main import "fmt" func main() { /* create a slice */ numbers := []int{0,1,2,3,4,5,6,7,8} /* print the numbers */ for i:= range numbers { fmt.Println("Slice item",i,"is",numbers[i]) } /* create a map*/ countryCapitalMap := map[string] string {"France":"Paris","Italy":"Rome","Japan":"Tokyo"} /* print map using keys*/ for country := range countryCapitalMap { fmt.Println("Capital of",country,"is",countryCapitalMap[country]) } /* print map using key-value*/ for country,capital := range countryCapitalMap { fmt.Println("Capital of",country,"is",capital) } }
๐ŸŒ
ZetCode
zetcode.com โ€บ golang โ€บ range
Using Range in Go
The following example uses range to iterate over a Go array. ... package main import "fmt" func main() { vals := [...]int{5, 4, 3, 2, 1} for idx, e := range vals { fmt.Println("element", e, "at index", idx) } }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ go language โ€บ range-keyword-in-golang
Range Keyword in Golang - GeeksforGeeks
July 12, 2025 - The range keyword is mainly used in for loops in order to iterate over all the elements of a map, slice, channel, or an array. When it iterates over the elements of an array and slices then it returns the index of the element in an integer form.
๐ŸŒ
Medium
medium.com โ€บ @ddwen โ€บ handling-large-arrays-in-golang-should-you-use-for-range-or-for-loop-9995a02fd316
Handling Large Arrays in Golang: Should You Use For Range or For Loop? | by Dwen | Medium
May 2, 2022 - But in order to facilitate Go developers to iterate on composite data types, such as arrays, slices, channels, and maps, Go provides a variant for range loop, which can even traverse maps and channels.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-for-range-loop-in-golang
What is the for-range loop in Golang?
A for loop is used to iterate over ... supports one additional form that uses the keyword range to iterate over an expression that evaluates to an array, slice, map, string, or channel....
๐ŸŒ
Zchee
zchee.github.io โ€บ golang-wiki โ€บ Range
Range Clauses - Go Programming Language Wiki
A range clause provides a way to iterate over an array, slice, string, map, or channel. for k, v := range myMap { log.Printf("key=%v, value=%v", k, v) } for v := range myChannel { log.Printf("value=%v", v) } for i, v := range myArray { log.Printf("array value at [%d]=%v", i, v) }
Top answer
1 of 2
12

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.

2 of 2
2

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

๐ŸŒ
Reddit
reddit.com โ€บ r/golang โ€บ how does for and range work together in golang?
r/golang on Reddit: How does for and range work together in golang?
December 5, 2025 - Hello guys I am building a CLI tool and I was writing a function that iterates over an array of elements (strings) that hold a filepath and basically just a simple test I wanted to see if I am able at all to open them so I wrote this: func TestMain (t *testing.T) { files := shared. GoFilesCrawler ("../") for file := range files { fileOp, err := os.
๐ŸŒ
Devtrovert
blog.devtrovert.com โ€บ go tricky: a bug with for-range loops and pointer
Go Tricky: A Bug with For-Range Loops and Pointer - Devtrovert
October 30, 2023 - for i := range even { fmt.Print(even[i], " ") } // 0x1400001a228 0x1400001a228 0x1400001a228