Assign string slice (from strings.SplitN) to multiple variables?

125 views
Skip to first unread message

Victor Hooi

unread,
Jul 24, 2015, 9:25:39 AM7/24/15
to golang-nuts
I'm using the following code to split a string on the "]" character:

message = strings.SplitN(scanner.Text(), "]", 2)[1]

I'd like to grab both parts of the SplitN(), and assign them like so:

preMessage, message = strings.SplitN(scanner.Text(), "]", 2)[1]

However, the above gives me an error message:

./redact.go:172: assignment count mismatch: 2 = 1

My question is, what is the idiomatic way to do the above assignment, in one line if possible? 

Some posts on StackOverflow (http://stackoverflow.com/questions/19832189/unpack-golang-slices-on-assignmenthttp://stackoverflow.com/questions/16551354/how-to-split-string-and-assign) seem to suggest this isn't possible (without creating your own assignment function), however, those posts are from two years ago, so I was curious if anything has changed since then, or somebody has come up with a more succinct way?

The other way I suppose is to use an intermediary variable:

x := strings.SplitN(scanner.Text(), "]", 2)
preMessage, message = x[0], x[1]

Is that considered the way to do it in Go?

Jan Mercl

unread,
Jul 24, 2015, 9:30:40 AM7/24/15
to golang-nuts
On Fri, Jul 24, 2015 at 3:25 PM Victor Hooi <victo...@gmail.com> wrote:

> x := strings.SplitN(scanner.Text(), "]", 2)
> preMessage, message = x[0], x[1]

> Is that considered the way to do it in Go? 

Yes. Modulo checking len(x) >= 2.

--

-j

Victor Hooi

unread,
Jul 24, 2015, 9:33:47 AM7/24/15
to golang-nuts, 0xj...@gmail.com
Hi,

Aha, ok, I'll do it that way then - just wasn't sure if having an intermediary variable like that was...wasteful?

Hmm, sorry, I don't quite understand the modulo checking part.

Modulo as in the modulus (%) operator?

Jan Mercl

unread,
Jul 24, 2015, 9:40:16 AM7/24/15
to golang-nuts
On Fri, Jul 24, 2015 at 3:33 PM Victor Hooi <victo...@gmail.com> wrote:

> Modulo as in the modulus (%) operator?

SplitN(..., 2) can return a slice of len < 2. Handling that case prevents a panic when executing the next line


        preMessage, message = x[0], x[1]
--

-j

Victor Hooi

unread,
Jul 24, 2015, 9:55:45 AM7/24/15
to golang-nuts, 0xj...@gmail.com
Ok, that makes sense -  although in this case, it shouldn't ever return more than 2 elements in the slice, right?

My apologies if this is all a bit basic - but would you be able to provide an example in this case of how to handle this error?

I understand that Go doesn't have assertions, and it's not like Python, where you have the whole "easier to ask forgiveness than permission" thing (i.e. try it and if it breaks, catch).

var preMessage, message string
x := strings.SplitN("Before the square bracket ] After the square bracket", "]", 2)
if len(x) == 2 {

preMessage, message = x[0], x[1]
fmt.Println("preMessage: " + preMessage + "\nmessage: " + message)
} else {
// throw something?
}

Also, where does the modulo (%) come in?

Konstantin Khomoutov

unread,
Jul 24, 2015, 10:09:11 AM7/24/15
to Victor Hooi, golang-nuts, 0xj...@gmail.com
On Fri, 24 Jul 2015 06:33:46 -0700 (PDT)
Victor Hooi <victo...@gmail.com> wrote:

> > > x := strings.SplitN(scanner.Text(), "]", 2)
> > > preMessage, message = x[0], x[1]
> > >
> > > Is that considered the way to do it in Go?
> >
> > Yes. Modulo checking len(x) >= 2.
>
> Aha, ok, I'll do it that way then - just wasn't sure if having an
> intermediary variable like that was...wasteful?
>
> Hmm, sorry, I don't quite understand the modulo checking part.
>
> Modulo as in the modulus (%) operator?

No, in this context "modulo" means "Except for (maybe missing)".
Please see the second definition in [1].

1. http://www.thefreedictionary.com/modulo
Reply all
Reply to author
Forward
0 new messages