How to get the last X Characters of a Golang String?

You can use a slice expression on a string to get the last three bytes.

s      := "12121211122"
first3 := s[0:3]
last3  := s[len(s)-3:]

Or if you’re using unicode you can do something like:

s      := []rune("世界世界世界")
first3 := string(s[0:3])
last3  := string(s[len(s)-3:])

Check Strings, bytes, runes and characters in Go and Slice Tricks.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)