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

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

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)
    }
}

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:

确定