使用 net/http 将结构值存储在每个请求 Golang 中
Struct value been stored in every request Golang using net/http
我是 Golang 的新手,我正在测试 net/http 到 运行 一些路径,但我遇到了一些我不明白的问题。
这是我的代码。
package main
import (
"fmt"
"net/http"
)
type Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fmt.Println("404");
return
}
fmt.Println("index content ", Content)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
Content.Data = data
fmt.Println("test content ", Content)
}
所以,如果我转到索引 http://localhost:8080/
,我得到的是空内容 index content {{false } map[]}
,
然后我转到 http://localhost:8080/test
我得到了正确的内容,test content {{false } map[data1:data 1 content data2:data 2 content]}
,
但是当我回到索引 http://localhost:8080/
那里已经有内容 index content {{false } map[data1:data 1 content data2:data 2 content]}
,
所以在这里提问,为什么我返回索引时没有得到空的结构内容?我认为每个请求的结构都会处于初始状态? http应该是无状态的吧?
您遇到的可能是这段代码或类似代码的结果(您的代码无法编译):
package main
import (
"fmt"
"net/http"
)
var Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fmt.Println("404")
return
}
fmt.Println("index content ", Content)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
Content.Data = data
fmt.Println("test content ", Content)
}
解决方案
这样您就创建了一个全局变量 Content
,它在调用网络服务器时保持其状态。您可能想要的是:
package main
import (
"fmt"
"net/http"
)
type Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
var c Content
if r.URL.Path != "/" {
fmt.Println("404")
return
}
fmt.Println("index content ", c)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
var c Content
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
c.Data = data
fmt.Println("test content ", c)
}
所做的更改
- 使
Content
成为您在示例中所做的类型(这样它就不再是全局变量,而是定义我们可以重用的类型)
- 在每个需要的调用中声明
Content
(不是全局的,因为我们不希望它在服务器调用中保留其内容)
精华
如果不先声明变量,就不能使用类型。这就是您的示例未构建的原因。如果你尝试 go 会抱怨 Content
不是一个表达式。
我是 Golang 的新手,我正在测试 net/http 到 运行 一些路径,但我遇到了一些我不明白的问题。
这是我的代码。
package main
import (
"fmt"
"net/http"
)
type Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fmt.Println("404");
return
}
fmt.Println("index content ", Content)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
Content.Data = data
fmt.Println("test content ", Content)
}
所以,如果我转到索引 http://localhost:8080/
,我得到的是空内容 index content {{false } map[]}
,
然后我转到 http://localhost:8080/test
我得到了正确的内容,test content {{false } map[data1:data 1 content data2:data 2 content]}
,
但是当我回到索引 http://localhost:8080/
那里已经有内容 index content {{false } map[data1:data 1 content data2:data 2 content]}
,
所以在这里提问,为什么我返回索引时没有得到空的结构内容?我认为每个请求的结构都会处于初始状态? http应该是无状态的吧?
您遇到的可能是这段代码或类似代码的结果(您的代码无法编译):
package main
import (
"fmt"
"net/http"
)
var Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fmt.Println("404")
return
}
fmt.Println("index content ", Content)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
Content.Data = data
fmt.Println("test content ", Content)
}
解决方案
这样您就创建了一个全局变量 Content
,它在调用网络服务器时保持其状态。您可能想要的是:
package main
import (
"fmt"
"net/http"
)
type Content struct {
Data map[interface{}]interface{}
}
func main() {
mux := http.NewServeMux()
mux.Handle("/favicon.ico", http.NotFoundHandler())
mux.HandleFunc("/", Index)
mux.HandleFunc("/test", Testhandler)
http.ListenAndServe(":8080", mux)
}
func Index(w http.ResponseWriter, r *http.Request) {
var c Content
if r.URL.Path != "/" {
fmt.Println("404")
return
}
fmt.Println("index content ", c)
}
func Testhandler(w http.ResponseWriter, r *http.Request) {
var c Content
data := make(map[interface{}]interface{})
data["data1"] = "data 1 content"
data["data2"] = "data 2 content"
c.Data = data
fmt.Println("test content ", c)
}
所做的更改
- 使
Content
成为您在示例中所做的类型(这样它就不再是全局变量,而是定义我们可以重用的类型) - 在每个需要的调用中声明
Content
(不是全局的,因为我们不希望它在服务器调用中保留其内容)
精华
如果不先声明变量,就不能使用类型。这就是您的示例未构建的原因。如果你尝试 go 会抱怨 Content
不是一个表达式。