go – golang编译错误:类型没有方法

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

go - golang compile error: type has no method

问题

如何解决这个问题?
https://play.golang.org/p/aOrqmDM91J

>:28: Cache.Segment未定义(Cache类型没有Segment方法)
>
>:29: Cache.Segment未定义(Cache类型没有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")
}
英文:

how to solve it?
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 &quot;fmt&quot;

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 &lt; 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(&quot;a01&quot;, &quot;111111&quot;)
}

答案1

得分: 3

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].Keyc.Segment[0].Val[mi],而不是Cache.Segment[0].KeyCache.Segment[0].Val[mi]

<kbd>Go Playground</kbd>

**无关的建议:**在你的代码上运行gofmt。它指出了违反Go代码常规风格指南的问题。我注意到你的代码中有一些问题。

英文:

Cache is a type. To call a method on a Cache object you have to do this.

func (c *Cache) Set(k string, v string) {
    for mi, _ := range c.Segment[0].Key {
         fmt.Println(c.Segment[0].Val[mi])  
    }
}

Note its c.Segment[0].Key and c.Segment[0].Val[mi] instead of Cache.Segment[0].Key and Cache.Segment[0].Val[mi]

<kbd>Go Playground</kbd>

unrelated suggestion: Run gofmt on your code. It points to violations of regularly followed style guidelines for go code. I notice a few on your code.

答案2

得分: -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

英文:

You need to give a variable to *Cache to use it, something like:

package main
import &quot;fmt&quot;

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 &lt; 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(&quot;a01&quot;, &quot;111111&quot;)
}

http://play.golang.org/p/1vLwVZrX20

huangapple
  • 本文由 发表于 2016年2月27日 11:36:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/35665362.html
匿名

发表评论

匿名网友

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

确定