重构 main.go 到其他包中。

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

Refactor main.go to another packages

问题

好的,我会为你翻译这段内容。请稍等片刻。

英文:

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.

答案1

得分: 1

我建议你按照golang之旅进行学习。

这里所解释的那样,Go代码由包组成,通常每个文件夹代表一个包。

正如Burak所回答的那样,你不能在声明它们实现的类型之外的包中创建方法

也就是说,如果你这样做:

  • baseFolder
    • main
      • main.go(包main)
    • config
      • config.go(包config)
      • routes.go(包config)
      • statusHandler.go(包config)

你可以在不引用"package"的情况下使用你的代码。

在main.go中导入它:

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

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

huangapple
  • 本文由 发表于 2022年3月26日 03:16:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/71622069.html
匿名

发表评论

匿名网友

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

确定