如何在Golang中从另一个文件中运行函数?

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

How to run the function from another file in golang?

问题

在这段代码中,我试图在浏览器中运行home函数。我创建了两个文件,main.gohandler.go。在main.go中,我调用了handler.Home函数来运行它。但是它给我返回了一个空白页面。

当我将handler.go文件中的Home()函数复制到main.go文件中时,它成功地运行了Home()函数。

main.go

package main

import (
	"fmt"
	"net/http"

	"github.com/ibilalkayy/WEBAPP1/handler"
)

func main() {
	http.HandleFunc("/", handler.Home)
	fmt.Println("Starting the server at :8080")
	http.ListenAndServe(":8080", nil)
}

handler.go

package handler

import (
	"html/template"
	"net/http"
)

var tmpl = make(map[string]*template.Template)

func init() {
	tmpl["home"] = template.Must(template.ParseFiles("templates/home.html", "templates/base.html"))
}

func Home(w http.ResponseWriter, r *http.Request) {
	tmpl["home"].ExecuteTemplate(w, "home.html", nil)
}

仅包含main.go的版本

package main

import (
	"fmt"
	"html/template"
	"net/http"
)

var tmpl = make(map[string]*template.Template)

func init() {
	tmpl["home"] = template.Must(template.ParseFiles("templates/home.html", "templates/base.html"))
}

func home(w http.ResponseWriter, r *http.Request) {
	tmpl["home"].ExecuteTemplate(w, "home.html", nil)
}

func main() {
	http.HandleFunc("/", home)
	fmt.Println("Starting the server at :8080")
	http.ListenAndServe(":8080", nil)
}

以上是你要翻译的内容。

英文:

In this code, I am trying to run the home function in a browser. I made two files, main.go and handler.go. In main.go I called the handler.Home function to run it. But it gives me the blank page.

When I take the Home() function from the handler.go file and use it in main.go file, it runs the Home() function successfully.

main.go

package main
 
import (
    "fmt"
    "net/http"

    "github.com/ibilalkayy/WEBAPP1/handler"
)

func main() {
	http.HandleFunc("/", handler.Home)
	fmt.Println("Starting the server at :8080")
	http.ListenAndServe(":8080", nil)
}

handler.go

package handler

import (
	"html/template"
	"net/http"
)
var tmpl = make(map[string]*template.Template)

func init() {
	tmpl["home"] = template.Must(template.ParseFiles("templates/home.html", "templates/base.html"))
}

func Home(w http.ResponseWriter, r *http.Request) {
	tmpl["home"].ExecuteTemplate(w, "home.html", nil)
}

main.go only version

package main

import (
	"fmt"
	"html/template"
	"net/http"
)

var tmpl = make(map[string]*template.Template)

func init() {
	tmpl["home"] = template.Must(template.ParseFiles("templates/home.html", "templates/base.html"))
}

func home(w http.ResponseWriter, r *http.Request) {
	tmpl["home"].ExecuteTemplate(w, "home.html", nil)
}

func main() {
	http.HandleFunc("/", home)
	fmt.Println("Starting the server at :8080")
	http.ListenAndServe(":8080", nil)
}

答案1

得分: 1

我刚刚复制了你的代码,并创建了一个具有以下目录结构的处理程序包。

如何在Golang中从另一个文件中运行函数?

它按预期工作。

英文:

I just copied your code and made a handler package with directory structure like follows.

如何在Golang中从另一个文件中运行函数?

It is working as expected.

huangapple
  • 本文由 发表于 2021年8月11日 20:20:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/68741911.html
匿名

发表评论

匿名网友

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

确定