动态变量是否被支持?

huangapple go评论129阅读模式
英文:

Are dynamic variables supported?

问题

我想知道在Go语言中是否可以动态创建变量?

我提供了下面的伪代码来说明我的意思。我将新创建的变量存储在一个切片中:

func method() {
  slice := make([]type)
  for i := 0; i < 10; i++ {
    var variable+i = i
    slice = append(slice, variable+i)
  }
}

循环结束时,切片应该包含变量:variable1、variable2...variable9。

英文:

I was wondering if it is possible to dynamically create variables in Go?

I have provided a pseudo-code below to illustrate what I mean. I am storing the newly created variables in a slice:

func method() {
  slice := make([]type)
  for(i=0;i&lt;10;i++)
  {
    var variable+i=i;
    slice := append(slice, variablei)
  }
}

At the end of the loop, the slice should contain the variables: variable1, variable2...variable9

答案1

得分: 15

Go语言没有动态变量。
大多数语言中的动态变量是通过Map(哈希表)实现的。

所以你可以在你的代码中使用以下其中一个Map来实现你想要的功能:

var m1 map[string]int 
var m2 map[string]string 
var m3 map[string]interface{}

这是一个实现你想要的功能的Go代码:

http://play.golang.org/p/d4aKTi1OB0

package main

import "fmt"


func method() []int {
  var  slice []int 
  for i := 0; i < 10; i++  {
    m1 := map[string]int{}
    key := fmt.Sprintf("variable%d", i)
    m1[key] = i
    slice = append(slice, m1[key])
  }
  return slice
}

func main() {
	fmt.Println(method())
}
英文:

Go has no dynamic variables.
Dynamic variables in most languages are implemented as Map (Hashtable).

So you can have one of following maps in your code that will do what you want

var m1 map[string]int 
var m2 map[string]string 
var m3 map[string]interface{}

here is Go code that does what you what

http://play.golang.org/p/d4aKTi1OB0

package main

import &quot;fmt&quot;


func method() []int {
  var  slice []int 
  for i := 0; i &lt; 10; i++  {
    m1 := map[string]int{}
    key := fmt.Sprintf(&quot;variable%d&quot;, i)
    m1[key] = i
    slice = append(slice, m1[key])
  }
  return slice
}

func main() {
	fmt.Println(method())
}

答案2

得分: 3

不;如果在编译时不知道局部变量的名称,就不能引用它们。

如果需要额外的间接引用,可以使用指针来实现。

func function() {
    slice := []*int{}
    for i := 0; i < 10; i++ {
        variable := i
        slice = append(slice, &variable)
    }
    // slice 现在包含了十个指向整数的指针
}

还要注意,for 循环中的括号应该省略,并且将左大括号放在新的一行会导致语法错误,因为在 ++ 后会自动插入分号。使用 make 创建切片需要传递一个长度,因此我没有使用它,因为无论如何都使用了 append

英文:

No; you cannot refer to local variables if you don’t know their names at compile-time.

If you need the extra indirection you can do it using pointers instead.

func function() {
    slice := []*int{}
    for i := 0; i &lt; 10; i++ {
        variable := i
        slice = append(slice, &amp;variable)
    }
    // slice now contains ten pointers to integers
}

Also note that the parentheses in the for loop ought to be omitted, and putting the opening brace on a new line is a syntax error due to automatic semicolon insertion after ++. makeing a slice requires you to pass a length, hence I don’t use it since append is used anyway.

huangapple
  • 本文由 发表于 2014年1月16日 04:54:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/21147978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定