将 main.go 重构到另一个包

发布于 2025-01-17 00:14:27 字数 1617 浏览 3 评论 0原文

好,首先,我正在学习围棋,我是一个聋子,英语不是我的主要语言。

我很难理解这些视频,尽管有字幕,但很多时候我都不太明白。尽管如此,我还是很想学。

我想了解为什么同一包(main)中的方法有效,例如:

main.go

type application struct {
    config config
    logger *log.Logger
}

paths.go

func (app *application) routes() *httprouter.Router {
    router := httprouter.New()

    router.HandlerFunc(http.MethodGet, "/status", app.statusHandler)
    
    return router
}

然后我像这样重构:

main.go

app := &config.Application{
    Config: cfg,
    Logger: logger,
}

config.go

type Application struct {
    Config Config
    Logger *log.Logger
}

paths.go

func (app *config.Application) Routes() *httprouter.Router {
    router := httprouter.New()

    router.HandlerFunc(http.MethodGet, "/status", app.statusHandler)

    return router
}

statusHandler.go

func (app *config.Application) StatusHandler(w http.ResponseWriter, r *http.Request) {
    currentStatus := config.AppStatus{
        Status:      "OK",
        Environment: app.Config.Env,
        Version:     app.Config.Version,
    }

    js, err := json.MarshalIndent(currentStatus, "", "\t")
    if err != nil {
        app.Logger.Println(err)
    }

    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusOK)
    w.Write(js)

}

但应用程序有下划线并说我:无效的接收器类型 *config.Application。

如果有人可以向我解释这个问题以及如何解决这个问题并使用方法或其他可接受的方式使其成为多模块,我将很高兴。

问候,佩德罗。

我尝试在另一个新项目中这样做,一步一步地做,以防我错过了什么。 然而,我发现我似乎对方法的概念不是很清楚。

Good, first of all, I am learning Go and I am a deaf person and English is not my main language.

I'm having a hard time understanding the videos, even though they are subtitled, I don't understand very well many times. Even so, I really want to learn.

I want to understand why methods in the same package (main) works, ex.:

main.go

type application struct {
    config config
    logger *log.Logger
}

routes.go

func (app *application) routes() *httprouter.Router {
    router := httprouter.New()

    router.HandlerFunc(http.MethodGet, "/status", app.statusHandler)
    
    return router
}

then I refactor like this:

main.go

app := &config.Application{
    Config: cfg,
    Logger: logger,
}

config.go

type Application struct {
    Config Config
    Logger *log.Logger
}

routes.go

func (app *config.Application) Routes() *httprouter.Router {
    router := httprouter.New()

    router.HandlerFunc(http.MethodGet, "/status", app.statusHandler)

    return router
}

statusHandler.go

func (app *config.Application) StatusHandler(w http.ResponseWriter, r *http.Request) {
    currentStatus := config.AppStatus{
        Status:      "OK",
        Environment: app.Config.Env,
        Version:     app.Config.Version,
    }

    js, err := json.MarshalIndent(currentStatus, "", "\t")
    if err != nil {
        app.Logger.Println(err)
    }

    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusOK)
    w.Write(js)

}

But app are underlined and say me: invalid receiver type *config.Application.

I would be glad if someone can explain me the problem and how to fix this and make it multi-module with methods or other accepted way.

Regards, Pedro.

I have tried to do it with another new project, doing it step by step in case I missed something.
However, I realized that I don't seem to be very clear on the concept of methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2025-01-24 00:14:27

我建议您关注 golang 之旅

正如这里所解释的,go代码是由包组成的,通常每个文件夹代表自己的包。

正如 Burak 所回答的,您无法在声明其实现的类型的包外部创建 方法

也就是说,如果您执行以下操作:

 - baseFolder
 |- main 
 |-- main.go (package main)
 |- config
 |-- config.go (package config)
 |-- routes.go (package config)
 |-- statusHandler.go (package config)

您可以使用您的代码而不引用“包”

func (app *Application) StatusHandler(w http.ResponseWriter, r *http.Request) {
...}

并将其导入您的 main.go

I recomend you following the golang tour.

As explained here go code is made by packages, usually each folder represents its own package.

As answerd by Burak you can't create Methods outside the package that declare the type that they implement.

That said if you do something like this:

 - baseFolder
 |- main 
 |-- main.go (package main)
 |- config
 |-- config.go (package config)
 |-- routes.go (package config)
 |-- statusHandler.go (package config)

You can use your code without referencing the "package"

func (app *Application) StatusHandler(w http.ResponseWriter, r *http.Request) {
...}

And import it on your main.go

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文