February 2, 2015

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 comments 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. Required fields are marked *

XFerion Europe LLP
We build an maintain your software. From web to mobile. From MVP to extended product. Contact us if you wish to discuss a project.
CONTACT US