How do I increment the index for display purposes only?

Viewed 73

This code starts printing the data from the 0 index but I want it to seem as if the index starts at 1.

Given Result

0 Foo
1 Bar

Required Result

1 Foo
2 Bar

Code

package main

import "fmt"

func main() {
    a := []string{"Foo", "Bar"}
    for i, s := range a {
        fmt.Println(i, s)
    }
}
1 Answers

The simplest way to achieve what you are looking for is by an increment as follows:

package main

import "fmt"

func main() {
    a := []string{"Foo", "Bar"}
    for i, s := range a {
        fmt.Println(i+1, s)
    }
}

Output:

1 Foo
2 Bar
Related