英文:
Golang claims my method is undefined, but it's RIGHT THERE
问题
我很难理解为什么Go无法识别来自其包外部的一个单独方法。这在其他使用包外方法时并没有发生。我对Go还比较新,所以我想知道是否有一些作用域方面的问题我没有注意到。
database.go
:
package database
type Database struct {
fileString string
}
db-table-person.go
:
package database
import (
"context"
)
type Person struct {
Name string
Age int
}
func (db *Database) AddPerson(ctx context.Context, person *Person) error {
// 添加一个人到数据库的代码
}
get-objects-from-file.go
:
package myapp
import (
"encoding/json"
"os"
"ioutil"
database "path/to/database/package/that/works/for/every/other/database/method"
)
type BundleOfObjects struct {
People []database.People
Dogs []database.Dogs
}
func LoadObjectsFromFile(filename string) (objects BundleOfObjects) {
jsonFile, _ := os.Open(filename)
defer jsonFile.Close()
bytevalue, _ := ioutil.ReadAll(jsonFile)
var objects BundleOfObjects
_ = json.Unmarshal(byteValue, &BundleOfObjects)
return BundleOfObjects
}
main.go
:
package main
import (
"context"
database "path/to/database/package/that/works/for/every/other/method/in/the/database/package/"
myapp "path/to/myapp/that/works/for/every/other/method/in/myapp"
)
func main() {
filename := "myfile.json"
bundleOfObjects := myapp.LoadObjectsFromFile(filename)
for _, p := range bundleOfObjects.People {
database.AddPerson(context.Background(), p)
}
}
我得到了以下错误,指向main.go
中对AddPerson
的调用:undefined: database.AddPerson
。
我再次强调,我已经成功地在这三个包中共享了许多方法,没有任何问题,似乎都使用了完全相同的过程。我漏掉了什么?
英文:
I'm having trouble understanding why Go isn't recognizing one singular method from outside of its package. This isn't happening with any other methods used outside of their packages. I am rather new to Go, so I'm wondering if there is some aspect of scope that I'm just missing.
database.go
:
package database
type Database struct {
fileString string
}
db-table-person.go
:
package database
import (
"context"
)
type Person struct {
Name string
Age int
}
func (db *Database) AddPerson(ctx context.Context, person *Person) error {
// code to add a person to a database
}
get-objects-from-file.go
:
package myapp
import (
"encoding/json"
"os"
"ioutil"
database "path/to/database/package/that/works/for/every/other/database/method"
)
type BundleOfObjects struct {
People []database.People
Dogs []database.Dogs
}
func LoadObjectsFromFile(filename string) (objects BundleOfObjects) {
jsonFile, _ := os.Open(filename)
defer jsonFile.Close()
bytevalue, _ := ioutil.ReadAll(jsonFile)
var objects BundleOfObjects
_ = json.Unmarshal(byteValue, &BundleOfObjects)
return BundleOfObjects
}
main.go
:
package main
import (
"context"
database "path/to/database/package/that/works/for/every/other/method/in/the/database/package/"
myapp "path/to/myapp/that/works/for/every/other/method/in/myapp"
)
func main() {
filename := "myfile.json"
bundleOfObjects := myapp.LoadObjectsFromFile(filename)
for _, p := range bundleOfObjects.People {
database.AddPerson(context.Background(), p)
}
}
I get the following error referencing the call to AddPerson
in main.go
: undefined: database.AddPerson
I cannot stress enough that I have successfully shared many methods throughout these three packages with zero issues whatsoever, all seemingly using this exact same process. What am I missing?
答案1
得分: 1
一如既往,在发布问题后的一两分钟内,答案就显而易见了。问题在于AddPerson()
是一个方法,而不仅仅是一个没有接收器的函数。它需要在现有的Database
实例上调用。因此,在main.go
中,我需要首先创建一个数据库实例,然后使用它来调用AddPerson()
。像这样:
main.go
:
package main
import (
"context"
database "path/to/database/package/that/works/for/every/other/method/in/the/database/package/"
myapp "path/to/myapp/that/works/for/every/other/method/in/myapp"
)
func main() {
filename := "myfile.json"
db := database.Database{fileString: "my-database.db"}
bundleOfObjects := myapp.LoadObjectsFromFile(filename)
for _, p := range bundleOfObjects.People {
db.AddPerson(context.Background(), p)
}
}
英文:
As always, just a minute or two after posting my question, the answer becomes apparent. The problem is that AddPerson()
is a method, not just a function without a receiver. It needs to be called on an existing instance of Database.
So in main.go
, I would need to create a database instance first and then use it to call AddPerson().
Like so:
main.go
:
package main
import (
"context"
database "path/to/database/package/that/works/for/every/other/method/in/the/database/package/"
myapp "path/to/myapp/that/works/for/every/other/method/in/myapp"
)
func main() {
filename := "myfile.json"
db := database.Database{fileString: "my-database.db"}
bundleOfObjects := myapp.LoadObjectsFromFile(filename)
for _, p := range bundleOfObjects.People {
db.AddPerson(context.Background(), p)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论