Logo

Programming-Idioms

History of Idiom 46 > diff from v21 to v22

Edit summary for version 22 by :
More specific playground link, Comment.

Version 21

2015-11-14, 22:04:58

Version 22

2015-11-17, 21:13:05

Idiom #46 Extract beginning of string (prefix)

Create string t consisting of the 5 first characters of string s.

Idiom #46 Extract beginning of string (prefix)

Create string t consisting of the 5 first characters of string s.

Code
t := string([]rune(s)[:5])
Code
t := string([]rune(s)[:5])
Comments bubble
This "incurs a run-time cost" proportional to len(s).
Doc URL
https://golang.org/ref/spec#Conversions
Doc URL
https://golang.org/ref/spec#Conversions
Demo URL
http://play.golang.org/p/AFkuQnah0q
Demo URL
http://play.golang.org/p/KvXiBYv9Y6