英文:
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 "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")
}
答案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].Key
和c.Segment[0].Val[mi]
,而不是Cache.Segment[0].Key
和Cache.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 "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")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论