go语言方法绑定到其他文件下的struct出错
mypackage 包已正确引入,包名和包路径也正确,且 mystruct 类型也已经在 mypackage 包中被正确定义
go的版本是1.18.1
报错无法引用:
目录:
test_struct
- mymethods
- mymethods.go
- mystructs
- mystructs.go
- main.go
mymethods.go:
package mymethods
import "test_struct/mystructs"
func (s *mystructs.MyStruct) MyMethod() {
// method implementation here
}
mystructs.go:
package mystructs
type MyStruct struct {
Field1 string
Field2 int
}
main.go:
package main
import (
"test_struct/mymethods"
"test_struct/mystructs"
)
func main() {
s := &mystructs.MyStruct{
Field1: "foo",
Field2: 42,
}
s.MyMethod()
}