golang导入结构体指针 [英] golang import struct pointer

查看:107
本文介绍了golang导入结构体指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个主包和一个http处理程序包。基本上我想要做的就是设置一个全局结构,这样我就可以随时调用该结构中的信息。



下面我尝试的示例的基本大纲:
主包导入处理函数
主包调用handlerfunc
Handlerfunc将http.ResponseWriter和其他项设置为UrlInfo struct
Handlerfunc运行在函数中传递(无需将UrlStruct传递到函数中)
运行函数(在本例中为home)
函数home可以随时调用变量uinfo,使其成为一个指针UrlInfo struct



显然,这并不代表工作,但这基本上是我想这样做,我不必将所有这些信息传递到我的家庭功能。保持干净简单。



欢迎任何想法和想法。

处理程序包

 程序包处理程序

//包含http请求和变量的struct
类型UrlInfo struct {
Res http.ResponseWriter
Req * http.Request
Item string
}

func HandleFunc(处理路径字符串,runfunc func()){
//设置处理程序和设置结构
http.HandleFunc(handlepath,func(w http.ResponseWriter,r * http.Request){
url:= new(UrlInfo)
url.Res = w
url.Req = r
url.Item =商品信息

runfunc()
})
}




 导入处理程序

var uinfo =& handler.UrlInfo {}

func init(){
handler.HandleFunc(/ home /,home)
}

func home(){
fmt.Println(uinfo.Item)
}


解决方案

从我收集的内容从你的问题中,你试图定义一个结构的单个全局实例,其中包含对当前Request和ResponseWriter的引用。



如果这是意图,我应该警告你这会导致问题。
Go的http包在单独的goroutine中执行每个请求处理程序。这意味着您可以同时处理任意多个请求。因此,他们不能全都参考相同的全球结构,并期望它包含仅与该特定请求相关的请求信息。如果您期望您的服务器是线程安全的,则不应使用全局实例。



通过在结构中分组无关参数来保持代码清洁可能非常方便,但在您的的情况下,我不相信你可以(或应该)避免将 UrlInfo 结构的新实例直接传递给 home()作为参数。它会使事情变得不必要的复杂和不可预测。


Ok i have a main package and a http handler package. Essentially what i am trying to do is setup a global struct so that way i can call upon information in that struct at any time.

Basic outline of my attempted example below: Main package imports handler function Main package calls handlerfunc Handlerfunc sets http.ResponseWriter and other items into UrlInfo struct Handlerfunc runs passed in function (without having to pass UrlStruct into function) Run function (home in this example) Function home can call upon variable uinfo at any time cause its a pointer UrlInfo struct

Obviously this doesnt work, but this is essentially what i would like to do so that way im not having to pass all this info into my home function. Keeping it clean and simple.

Any thoughts and ideas are welcome. Thanks.

Handler Package

package handler

// Struct containing http requests and variables
type UrlInfo struct {
    Res http.ResponseWriter
    Req *http.Request
    Item string
}

func HandleFunc(handlepath string, runfunc func()) {
    // Set handler and setup struct
    http.HandleFunc(handlepath, func(w http.ResponseWriter, r *http.Request) {
        url := new(UrlInfo)
        url.Res = w
        url.Req = r
        url.Item = "Item information"

        runfunc()
    })
}

Main Package

import "handler"

var uinfo = &handler.UrlInfo{}

func init() {
    handler.HandleFunc("/home/", home)
}

func home() {
    fmt.Println(uinfo.Item)
}

解决方案

From what I gather from your question, you are attempting to define a single, global instance of a structure which, among other things, holds a reference to the current Request and ResponseWriter.

If this is the intention, I should warn you this is going to cause problems. Go's http package executes each request handler in a separate goroutine. This means that you can have arbitrarily many requests being handled simultaneously. Therefore they can not all refer to the same global structure safely and expect it to contain request information relevant only to that particular request. The global instance should not be used if you expect your server to be thread safe.

Keeping the code clean by grouping extraneous parameters in a structure can be handy, but in your case, I do not believe you can (or should) avoid passing a new instance of UrlInfo structure directly to home() as a parameter. It will make things unnecessarily complex and unpredictable.

这篇关于golang导入结构体指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆