irom
irom

Reputation: 3606

Python list comprehension in Go

I'm translating below Python function to Go. It's using list comprehension which I don't think is available in Go. What's the best way to translate it to Go ?

def list1s():
        return ["10." + str(x) + "." + str(y) + ".1" for x in range(192, 256) for y in range(0, 256)]

Upvotes: 1

Views: 716

Answers (1)

T. Claverie
T. Claverie

Reputation: 12256

Simply use explicit for loops. However, you should not try to simply translate what you do in a language into another.

func list1s() []string {
    res := make([]string, 0, 256*64)
    for x := 192; x < 256; x++ {
        for y := 0; y < 256; y++ {
            res = append(res, fmt.Sprintf("10.%d.%d.1", x, y))
        }
    }
    return res
}

Upvotes: 1

Related Questions