Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在go lang中导出变量/属性_Go_Package_Webserver - Fatal编程技术网

如何在go lang中导出变量/属性

如何在go lang中导出变量/属性,go,package,webserver,Go,Package,Webserver,我正在尝试在golang中创建一个MVC web应用程序,而不使用任何框架。我计划如何实现它是创建一个http.Server{}的实例,其处理程序为http.NewServeMux(),代码如下: sm := http.NewServeMux() sm.Handle("/route1", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "static

我正在尝试在golang中创建一个MVC web应用程序,而不使用任何框架。我计划如何实现它是创建一个http.Server{}的实例,其处理程序为http.NewServeMux(),代码如下:

 sm := http.NewServeMux()
    sm.Handle("/route1", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, "static/front-office/index.html")
    }))
    sm.Handle("/route2", handleSomething())
    sm.Handle("/route3", handleSomething())
    sm.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))


    frontEndUIServer := http.Server{
        Addr:    ":9000",
        Handler:  sm,
    }
    go frontEndUIServer.ListenAndServe()
然后使属性
sm
可导出,以便任何其他go文件都可以导入它并在其上创建处理程序,从而实现我的控制器。由于我是刚来goLang的新手,我现在的问题是如何使属性
sm
可导出?

当你问“如何使属性sm可导出”时,我想你的意思是节点意义上的?如果是这样的话,你要寻找的概念就是“包”

这允许使用“导入”在其他包中引用一个包中的功能。请注意,要访问的函数名必须以大写字母vs小写字母开头,该字母只能与包一起引用

通常,web服务器在“主”功能/包中创建,控制器连接到您定义的路由


这是一个很好的基本示例:

您可以在
mywebapp
包中定义您的web应用,如下所示:

打包mywebapp
导入“net/http”
var SM*http.ServeMux
func init(){
SM=http.NewServeMux()
SM.Handle(“/route1”,http.HandlerFunc(func(w http.ResponseWriter,r*http.Request){
http.ServeFile(w,r,“static/front office/index.html”)
}))
SM.Handle(“/route2”,handleSomething())
SM.Handle(“/route3”,handleSomething())
SM.Handle(“/static/”,http.StripPrefix(“/static/”,http.FileServer(http.Dir(“static”)))
frontEndUIServer:=http.Server{
地址:“:9000”,
经办人:SM,
}
转到frontEndUIServer.listendServe()
}
导出服务器使用的
ServeMux
,以便其他包可以添加处理程序。导入包后,服务器立即启动