如何解决? https://play.golang.org/p/aOrqmDM91J
:28: Cache.Segment undefined (type Cache has no method Segment)
:29: Cache.Segment undefined (type Cache has no method Segment)
package main
import "fmt"
type Slot struct {
Key []string
Val []string
}
type Cache struct{
Segment [3615]Slot
}
func NewCache(s int) *Cache{
num:=3615
Cacheobj:=new(Cache)
for i := 0; i < num; i++ {
Cacheobj.Segment[i].Key = make([]string, s)
Cacheobj.Segment[i].Val = make([]string, s)
}
return Cacheobj
}
func (*Cache)Set(k string, v string) {
for mi, mk := range Cache.Segment[0].Key {
fmt.Println(Cache.Segment[0].Val[mi])
}
}
func main() {
Cache1:=NewCache(100)
Cache1.Set("a01", "111111")
}
最佳答案
Cache
是一种类型。要在 Cache
对象上调用方法,您必须这样做。
func (c *Cache) Set(k string, v string) {
for mi, _ := range c.Segment[0].Key {
fmt.Println(c.Segment[0].Val[mi])
}
}
注意它的 c.Segment[0].Key
和 c.Segment[0].Val[mi]
而不是 Cache.Segment[0] .Key
和 Cache.Segment[0].Val[mi]
不相关的建议:运行gofmt在你的代码上。它指出违反了定期遵循的 go 代码风格指南。我注意到您的代码中有一些内容。
关于go-golang编译错误: type has no method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35665362/