golang 导入结构指针

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

golang import struct pointer

问题

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

下面是我尝试的示例的基本概述:
主包导入处理程序函数
主包调用handlerfunc
Handlerfunc将http.ResponseWriter和其他项目设置到UrlInfo结构体中
Handlerfunc运行传入的函数(无需将UrlStruct传递给函数)
运行函数(在这个示例中是home函数)
home函数可以随时调用变量uinfo,因为它是一个指向UrlInfo结构体的指针

显然这样是行不通的,但这基本上是我想做的,这样我就不必将所有这些信息传递给我的home函数。保持简洁和简单。

欢迎任何想法和建议。谢谢。

处理程序包

package handler

// 包含http请求和变量的结构体
type UrlInfo struct {
    Res http.ResponseWriter
    Req *http.Request
    Item string
}

func HandleFunc(handlepath string, runfunc func()) {
    // 设置处理程序并设置结构体
    http.HandleFunc(handlepath, func(w http.ResponseWriter, r *http.Request) {
        url := new(UrlInfo)
        url.Res = w
        url.Req = r
        url.Item = "Item信息"

        runfunc()
    })
}

主包

import "handler"

var uinfo = &handler.UrlInfo{}

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

func home() {
    fmt.Println(uinfo.Item)
}
英文:

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
<!-- languages: lang-go -->

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 = &quot;Item information&quot;

		runfunc()
	})
}

Main Package
<!-- languages: lang-go -->

import &quot;handler&quot;

var uinfo = &amp;handler.UrlInfo{}

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

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

答案1

得分: 5

从你的问题中我了解到,你正在尝试定义一个单一的全局结构实例,其中包含对当前请求和ResponseWriter的引用。

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

通过将多余的参数分组到一个结构中来保持代码清晰是很方便的,但在你的情况下,我不认为你可以(或者应该)避免直接将UrlInfo结构的新实例作为参数直接传递给home()。这将使事情变得不必要地复杂和不可预测。

英文:

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.

huangapple
  • 本文由 发表于 2012年9月29日 06:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/12648104.html
匿名

发表评论

匿名网友

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

确定