0

I am new to Go. I have 2 questions:

  1. Is there a way to get all the numbers between a range in Go? I can do range(1, 10) in Python or 1 to 10 in Scala to get a range.

  2. How to get all the alphabets in go? Like Python's string.letters and string.ascii_lower.

Amit Tripathi
  • 7,003
  • 6
  • 32
  • 58
  • 2
    What do you want to do? You can define any of those very easily. – JimB Jan 13 '17 at 18:47
  • You can the answers here: 1. http://stackoverflow.com/questions/21950244/is-there-a-way-to-iterate-over-a-range-of-integers-in-golang 2. http://stackoverflow.com/questions/17575840/better-way-to-generate-array-of-all-letters-in-the-alphabet – Luiz de Prá Jan 13 '17 at 18:50
  • @JimB Writing an application that involves base62 encoding. Yes, I can but I believe this should be included in language. I don't want to define every time I need those. Isn't its available in Go? – Amit Tripathi Jan 13 '17 at 18:50
  • @LuizdePrá I don't know how those questions are related to this. I don't want to iterate over a range. I want to get a list of numbers/letters between a range in Go. – Amit Tripathi Jan 13 '17 at 18:54
  • 3
    I don't think anyone considers these things as critical as you do. https://play.golang.org/p/ruV_CjSK5r. I think the problem may lie more in trying to write python in Go, rather than getting the values you need. Having a slice of ints or a string of ascii letters wouldn't necessarily be used in Go like you use it in python. – JimB Jan 13 '17 at 18:56
  • 1
    Possible duplicate of [Generate array with with range of integers](http://stackoverflow.com/questions/35704948/generate-array-with-with-range-of-integers/35705756#35705756). – icza Jan 13 '17 at 18:59
  • 1
    @JimB I am used to convenient way of doing things in Python maybe thats why even after trying to search on Google and failing to find, I was not ready to accept that a modern language would not have this "essential" feature. So, I asked here. I think I need to switch to Go way of doing things. Thanks :) – Amit Tripathi Jan 13 '17 at 19:02

1 Answers1

4

Go doesn't provide a lot of syntactic sugar. You just have to write what those Python and Scala functions do for you.

for i := 1; i <= 10; i++ {
    fmt.Print(i)
}
for i := 'A'; i <= 'Z'; i++ {
    fmt.Printf("%c", i)
}
for i := 'a'; i <= 'z'; i++ {
    fmt.Printf("%c", i)
}

12345678910ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

https://play.golang.org/p/SU0uFVIg0k

wgoudsbloem
  • 178
  • 5