Go: 无法在 “Struct” 的指针上调用指针方法 getFirstName

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

Go: cannot call pointer method getFirstName on "Struct"

问题

我有一个简单的结构体和接收器。我试图用结构体设置映射,然后调用接收器。

像这样:

package main

import (
	"fmt"
)

type myStruct struct {
	FirstName string 
	LastName string
}

func (m *myStruct) GetFirstName() string {
	return m.FirstName
}

func (m *myStruct) GetLastName() string {
	return m.LastName
}


func main() {
	


	testMyStruct := myStruct {
		FirstName: "x1",
		LastName: "x2",
	}

	myMapStruct["test2"] = testMyStruct 

	 

	fmt.Println(myMapStruct["test2"].GetFirstName())


}

我得到了这个错误:

 无法在myStruct上调用指针方法getFirstName

为什么我可以从映射中调用接收器方法?

英文:

I have a simple struct and receiver. I try to set the map with the struct and then call the receiver.

Like that:

package main

import (
	"fmt"
)

type myStruct struct {
	FirstName string 
	LastName string
}

func (m *myStruct) GetFirstName() string {
	return m.FirstName
}

func (m *myStruct) GetLastName() string {
	return m.LastName
}


func main() {
	


	testMyStruct := myStruct {
		FirstName: "x1",
		LastName: "x2",
	}

	myMapStruct["test2"] = testMyStruct 

	 

	fmt.Println(myMapStruct["test2"].GetFirstName())


}

I'm getting this error:

 cannot call pointer method getFirstName on myStruct

Why can I call the receiver method from the map?

答案1

得分: 2

由于您传递了一个receiver指针,您必须使用指向结构体的指针来调用它,请查看下面的代码:

package main

import (
    "fmt"
)

type myStruct struct {
    FirstName string 
    LastName string
}

func (m *myStruct) GetFirstName() string {
    return m.FirstName
}

func (m *myStruct) GetLastName() string {
    return m.LastName
}


func main() {



    testMyStruct := &myStruct {
        FirstName: "x1",
        LastName: "x2",
    }

    myMapStruct := make(map[string]*myStruct)

    myMapStruct["test2"] = testMyStruct



    fmt.Println(myMapStruct["test2"].GetFirstName())

}

在此处查看工作示例:链接

或者,如果将receiver作为值传递,可以使用以下方式:

package main

import (
    "fmt"
)

type myStruct struct {
    FirstName string 
    LastName string
}

func (m myStruct) GetFirstName() string {
    return m.FirstName
}

func (m myStruct) GetLastName() string {
    return m.LastName
}


func main() {



    testMyStruct := myStruct {
        FirstName: "x1",
        LastName: "x2",
    }

    myMapStruct := make(map[string]myStruct)

    myMapStruct["test2"] = testMyStruct



    fmt.Println(myMapStruct["test2"].GetFirstName())

}

英文:

As you are passing a receiver pointer ,you have to use pointer to the struct to call it ,Please look at the below code

package main

import (
    "fmt"
)

type myStruct struct {
    FirstName string 
    LastName string
}

func (m *myStruct) GetFirstName() string {
    return m.FirstName
}

func (m *myStruct) GetLastName() string {
    return m.LastName
}


func main() {
    


    testMyStruct := &myStruct {
        FirstName: "x1",
        LastName: "x2",
    }

  myMapStruct:=make(map[string]*myStruct)

    myMapStruct["test2"] = testMyStruct 

     

    fmt.Println(myMapStruct["test2"].GetFirstName())



}

See working here

or You can use like below if passing receiver as value

package main

import (
    "fmt"
)

type myStruct struct {
    FirstName string 
    LastName string
}

func (m myStruct) GetFirstName() string {
    return m.FirstName
}

func (m myStruct) GetLastName() string {
    return m.LastName
}


func main() {
    


    testMyStruct := myStruct {
        FirstName: "x1",
        LastName: "x2",
    }

  myMapStruct:=make(map[string]myStruct)

    myMapStruct["test2"] = testMyStruct 

     

    fmt.Println(myMapStruct["test2"].GetFirstName())



}

huangapple
  • 本文由 发表于 2022年6月24日 15:09:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/72740192.html
匿名

发表评论

匿名网友

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

确定