在任务中解压Golang切片? [英] Unpack Golang slices on assignment?

查看:181
本文介绍了在任务中解压Golang切片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Golang有没有一种优雅的方式可以像Python一样从数组中进行多项赋值?这是我想要做的一个Python例子(分割一个字符串,然后将结果数组分配给两个变量)。

  python:
>>> a,b =foo; bar.split(;)

我目前的解决方案是:

  x:= strings.Split(foo; bar,;)
a,b:= x [0],x [1]

我可以看到这在一些结构中变得混乱。我目前面临的实际示例是书签文件解析并分配给地图:

 书签:= make(map [字符串]字符串)
x:= strings.Split(foo \thttps:// bar,\t)
name,link:= x [0],x [1]
书签[name] = link

现在我有一个无用的变量x我想做一些事情:

$ $ p $ $ $ $ $ $ $ $ line:= strings.Split(foo \thttps:// bar,\t)
bookmark [name] = link

但这是无效的。

不支持在Python中完成解包。我认为去那里的方法是使用多个返回值定义您自己的小型临时函数:

  func splitLink(s ,sep string)(string,string){
x:= strings.Split(s,sep)
return x [0],x [1]
}

然后您可以编写:

  name,link:= splitLink(foo \thttps:// bar,\t)

但是,只有当至少有两个子字符串被分割时,这显然才起作用,并且如果两个以上的字符串被默默地忽略。如果这是你使用很多的东西,它可能会让你的代码更具可读性。

- 编辑 -



解包一个数组的另一种方法是通过variadic指针参数:

  func unpack(s [] string,vars ... * string){
for i,str:= range s {
* vars [i] = str
}
}

您可以写:

  var name,link string 
unpack(strings.Split(foo\thttps:// bar,\ t),& name,&link)
书签[name] = link

这适用于任何数组大小,但可以说它的可读性较差,你必须明确地声明你的变量。


Is there an elegant way in Golang to do multiple assignments from arrays like in Python? Here is a Python example of what I'm trying to do (split a string and then assign the resulting array into two variables).

python:
>>> a, b = "foo;bar".split(";")

My current solution is:

x := strings.Split("foo;bar", ";")
a, b := x[0], x[1]

I'm can see this getting messy in some constructs. The practical example I'm currently facing is a bookmark file parsing and assigning to a map:

bookmark := make(map[string]string)
x := strings.Split("foo\thttps://bar", "\t")
name, link := x[0], x[1]
bookmark[name] = link

Now I have a useless variable "x" sitting around. I'd like to do something like:

bookmark := make(map[string]string)
name, line := strings.Split("foo\thttps://bar", "\t")
bookmark[name] = link

but that's invalid.

解决方案

As Sergio Tulentsev mentioned, general packing/unpacking as is done in Python is not supported. I think the way to go there is to define your own small ad-hoc function using multiple return values:

func splitLink(s, sep string) (string, string) {
    x := strings.Split(s, sep)
    return x[0], x[1]
}

And you can then write:

name, link := splitLink("foo\thttps://bar", "\t")

But this will obviously work only when at least two substrings are being split, and silently ignore if more than two were. If this is something you use a lot, it might make your code more readable though.

--EDIT--

Another way to unpack an array is via variadic pointer arguments:

func unpack(s []string, vars... *string) {
    for i, str := range s {
        *vars[i] = str
    }
}

Which let you write:

var name, link string
unpack(strings.Split("foo\thttps://bar", "\t"), &name, &link)
bookmarks[name] = link

This will work for any array size, but it is arguably less readable, and you have to declare your variables explicitly.

这篇关于在任务中解压Golang切片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆