2 回答
TA贡献1783条经验 获得超5个赞
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].Keyandc.Segment[0].Val[mi]而不是Cache.Segment[0].KeyandCache.Segment[0].Val[mi]
Go Playground
不相关的建议:在您的代码上运行gofmt。它指出违反了经常遵循的 go 代码风格指南。我注意到你的代码中的一些。
TA贡献2011条经验 获得超2个赞
您需要为 *Cache 提供一个变量才能使用它,例如:
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 (c *Cache)Set(k string, v string) {
for mi, _:= range c.Segment[0].Key { // Had to change mk to _ because go will not compile when variables are declared and unused
fmt.Println(c.Segment[0].Val[mi])
}
}
func main() {
Cache1:=NewCache(100)
Cache1.Set("a01", "111111")
}
http://play.golang.org/p/1vLwVZrX20
- 2 回答
- 0 关注
- 198 浏览
添加回答
举报
