Golang substr or substring equivalent

Strings are slices in Google Go, so there is really no need for a Golang substr function. For example if you would want to get the first 10 characters of a string:

dt := "2015-01-01 15:45:12"
d := dt[0:10]

The resulting value in d will be: “2015-01-01”.

3 thoughts on “Golang substr or substring equivalent”

    1. Correct, if you use multi byte chars, you have to use runes:

      for i, ch := range “Japan 日本” {
      fmt.Printf(“%d:%q “, i, ch)
      }
      // Output: 0:’J’ 1:’a’ 2:’p’ 3:’a’ 4:’n’ 5:’ ‘ 6:’日’ 9:’本’

Leave a Reply

Your email address will not be published.