我有一个切片的类型别名.并且我希望能够在切片是指针接收器时附加到切片(或从切片中过滤):
package main
import (
"fmt"
)
type itself []string
func (h itself) appendToItself(test string) {
h = append(h, test)
}
func main() {
h := itself{"1", "2"}
h.appendToItself("3")
fmt.Println(h, "<- how do I make it [1,2,3]")
}
Run Code Online (Sandbox Code Playgroud)
日志:
[1 2] <- how do I make it [1,2,3]
Run Code Online (Sandbox Code Playgroud)
Aka*_*all 25
你需要实际传递指针,尝试:
package main
import (
"fmt"
)
type itself []string
func (h *itself) appendToItself(test string) {
*h = append(*h, test)
}
func main() {
h := itself{"1", "2"}
h.appendToItself("3")
fmt.Println(h, "<- how do I make it [1,2,3]")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6072 次 |
| 最近记录: |