irom irom - 6 months ago 10
Python Question

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)]

Answer

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, 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
}