Golang声称我的方法未定义,但它就在那里。

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

Golang claims my method is undefined, but it's RIGHT THERE

问题

我很难理解为什么Go无法识别来自其包外部的一个单独方法。这在其他使用包外方法时并没有发生。我对Go还比较新,所以我想知道是否有一些作用域方面的问题我没有注意到。

database.go:

  1. package database
  2. type Database struct {
  3. fileString string
  4. }

db-table-person.go:

  1. package database
  2. import (
  3. "context"
  4. )
  5. type Person struct {
  6. Name string
  7. Age int
  8. }
  9. func (db *Database) AddPerson(ctx context.Context, person *Person) error {
  10. // 添加一个人到数据库的代码
  11. }

get-objects-from-file.go:

  1. package myapp
  2. import (
  3. "encoding/json"
  4. "os"
  5. "ioutil"
  6. database "path/to/database/package/that/works/for/every/other/database/method"
  7. )
  8. type BundleOfObjects struct {
  9. People []database.People
  10. Dogs []database.Dogs
  11. }
  12. func LoadObjectsFromFile(filename string) (objects BundleOfObjects) {
  13. jsonFile, _ := os.Open(filename)
  14. defer jsonFile.Close()
  15. bytevalue, _ := ioutil.ReadAll(jsonFile)
  16. var objects BundleOfObjects
  17. _ = json.Unmarshal(byteValue, &BundleOfObjects)
  18. return BundleOfObjects
  19. }

main.go:

  1. package main
  2. import (
  3. "context"
  4. database "path/to/database/package/that/works/for/every/other/method/in/the/database/package/"
  5. myapp "path/to/myapp/that/works/for/every/other/method/in/myapp"
  6. )
  7. func main() {
  8. filename := "myfile.json"
  9. bundleOfObjects := myapp.LoadObjectsFromFile(filename)
  10. for _, p := range bundleOfObjects.People {
  11. database.AddPerson(context.Background(), p)
  12. }
  13. }

我得到了以下错误,指向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:

  1. package database
  2. type Database struct {
  3. fileString string
  4. }

db-table-person.go:

  1. package database
  2. import (
  3. "context"
  4. )
  5. type Person struct {
  6. Name string
  7. Age int
  8. }
  9. func (db *Database) AddPerson(ctx context.Context, person *Person) error {
  10. // code to add a person to a database
  11. }

get-objects-from-file.go:

  1. package myapp
  2. import (
  3. "encoding/json"
  4. "os"
  5. "ioutil"
  6. database "path/to/database/package/that/works/for/every/other/database/method"
  7. )
  8. type BundleOfObjects struct {
  9. People []database.People
  10. Dogs []database.Dogs
  11. }
  12. func LoadObjectsFromFile(filename string) (objects BundleOfObjects) {
  13. jsonFile, _ := os.Open(filename)
  14. defer jsonFile.Close()
  15. bytevalue, _ := ioutil.ReadAll(jsonFile)
  16. var objects BundleOfObjects
  17. _ = json.Unmarshal(byteValue, &BundleOfObjects)
  18. return BundleOfObjects
  19. }

main.go:

  1. package main
  2. import (
  3. "context"
  4. database "path/to/database/package/that/works/for/every/other/method/in/the/database/package/"
  5. myapp "path/to/myapp/that/works/for/every/other/method/in/myapp"
  6. )
  7. func main() {
  8. filename := "myfile.json"
  9. bundleOfObjects := myapp.LoadObjectsFromFile(filename)
  10. for _, p := range bundleOfObjects.People {
  11. database.AddPerson(context.Background(), p)
  12. }
  13. }

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

  1. package main
  2. import (
  3. "context"
  4. database "path/to/database/package/that/works/for/every/other/method/in/the/database/package/"
  5. myapp "path/to/myapp/that/works/for/every/other/method/in/myapp"
  6. )
  7. func main() {
  8. filename := "myfile.json"
  9. db := database.Database{fileString: "my-database.db"}
  10. bundleOfObjects := myapp.LoadObjectsFromFile(filename)
  11. for _, p := range bundleOfObjects.People {
  12. db.AddPerson(context.Background(), p)
  13. }
  14. }
英文:

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:

  1. package main
  2. import (
  3. "context"
  4. database "path/to/database/package/that/works/for/every/other/method/in/the/database/package/"
  5. myapp "path/to/myapp/that/works/for/every/other/method/in/myapp"
  6. )
  7. func main() {
  8. filename := "myfile.json"
  9. db := database.Database{fileString: "my-database.db"}
  10. bundleOfObjects := myapp.LoadObjectsFromFile(filename)
  11. for _, p := range bundleOfObjects.People {
  12. db.AddPerson(context.Background(), p)
  13. }
  14. }

huangapple
  • 本文由 发表于 2023年6月21日 04:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76518348.html
匿名

发表评论

匿名网友

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

确定