切片类型的初始化方法无法初始化

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

Initialization method for slice type fails to initialize

问题

我试图为我的自定义类型实现一个初始化方法。然而,在调用该方法后,变量在main()函数中没有改变。我可能还没有完全理解切片的工作原理,下面是我的示例代码:

package main
import "fmt"

type test [][]float64

func (p *test) init(m, n int){
    tmp := *p
    tmp = make(test, m)
    for i := 0; i < m; i++ {
        tmp[i] = make([]float64, n)
    }
}

func main(){
    var t test
    t.init(10,2)
    fmt.Println(t)
}

我原本以为接收者类型的内容可以被改变,但是对于切片来说似乎并非如此。那么,我该如何正确地将初始化函数绑定到我的类型上呢?我相当确定这是我方面的一个误解...

我尝试了几种方法,比如:

var t *test = new(test)

或者

func (p *test) init(m, n int){
    tmp := *p
    tmp = append(tmp, make(test, m)...)
    for i := 0; i < m; i++ {
        tmp[i] = append(tmp[i], make([]float64, n)...)
    }
}

等等,但都失败了...

我目前唯一知道的可行解决方案是一个无绑定方法,它返回一个指向新切片的指针。这暂时可以解决问题,但我想将其作为接口的先决条件。那么,我该如何绑定它呢?

英文:

I try to implement an initialization method for my own type. However after calling the method the variable is unchanged in main(). I probably haven't fully understood how slices work, here is my example code

package main
import &quot;fmt&quot;

type test [][]float64

func (p *test) init(m, n int){
    tmp := *p
    tmp = make(test, m)
    for i := 0; i &lt; m; i++ {
        tmp[i] = make([]float64, n)
    }
}

func main(){
    var t test
    t.init(10,2)
    fmt.Println(t)
}

I was under the impression that the content of the receiver type * can be changed, but this doesn't seem to be the case for slices. So how do I properly bind my initialization function to my type? I am pretty sure that there is an misunderstanding on my side....
I tried several things like

var t *test = new(test)

or

func (p *test) init(m, n int){
    tmp := *p
    tmp = append(tmp, make(test, m)...)
    for i := 0; i &lt; m; i++ {
        tmp[i] = append(tmp[i], make([]float64, n)...)
    }
}

and so on but all failed..

The only working solution I currently know of is a unbound method which returns the pointer to a fresh slice. This will do for now, but I want to make this a prerequisite in an interface. So how can I bind it?

答案1

得分: 8

func (p *test) init(m, n int) {
tmp := make(test, m)
for i := 0; i < m; i++ {
tmp[i] = make([]float64, n)
}
*p = tmp
}

你离答案很接近。上面的代码可以实现你想要的功能。但是没有理由避免返回一个新的切片的函数。这是惯用的写法,感觉上很像在其他语言中编写构造函数:

func newTest(m, n int) test {
t := make(test, m)
for i := range t {
t[i] = make([]float64, n)
}
return t
}

英文:
func (p *test) init(m, n int){
    tmp = make(test, m)
    for i := 0; i &lt; m; i++ {
        tmp[i] = make([]float64, n)
    }
    *p = tmp
}

You were close. The above does what you want. But there's no reason to avoid a function that returns a fresh slice. That's idiomatic and feels much like writing a constructor in other languages:

func newTest(m, n int) test {
    t = make(test, m)
    for i := range t {
        t[i] = make([]float64, n)
    }
    return t
}

huangapple
  • 本文由 发表于 2012年4月6日 00:29:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/10032400.html
匿名

发表评论

匿名网友

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

确定