Learning Go, what a great language.
Is there a built-in means to remove the first item in an array? Kind of like PHP's array_shift
I have a string, "the brown fox jumps"
I've found strings.Fields()
which turns it into an array. I'd like to turn that string into two strings:
"the", "brown fox jumps"
words := strings.Fields(theFoxString)
firstWord := // unshift first word from words
otherWords := // join what's left of words with ' '
Thank you for your help!
If we have any slice a
, we can do this:
x, a := a[0], a[1:]
So using your code, we get:
words := strings.Fields(theFoxString)
firstWord, otherWords := words[0], words[1:]
Keep in mind the underlying array hasn't changed, but the slice we are using to look at that array has. For most purposes this is ok (and even advantageous performance wise!), but it is something to be aware of.