Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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
http函数Golang的附加参数_Go - Fatal编程技术网

http函数Golang的附加参数

http函数Golang的附加参数,go,Go,在给定的示例中,我试图将字符串传递给处理程序 package main import ( "fmt" "net/http" ) func handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) } func main() { http.HandleFunc("/", handler) http.ListenAn

在给定的示例中,我试图将字符串传递给处理程序

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

func handler(w http.ResponseWriter, r *http.Request) {
     fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
     http.HandleFunc("/", handler)
     http.ListenAndServe(":8080", nil)
}
以下是我尝试过的,但它会抛出一个错误,因为它需要固定数量的参数:

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

func handler(w http.ResponseWriter, r *http.Request, s *string) {
     fmt.Fprintf(w, "Hi there, I love %s!", *s)
}

func main() {
     files := "bar"
     http.HandleFunc("/", handler(&files))
     http.ListenAndServe(":8080", nil)
}

这里有很多选择,您可以:

  • 使用闭包设置封闭处理程序的状态
  • 在处理程序的结构上使用方法,并在其中设置全局状态
  • 使用请求上下文存储值,然后将其取出
  • 使用包全局存储值
  • 用新的签名编写自己的路由器(不像听起来那么复杂,但可能不是个好主意)
  • 编写一个helper函数来执行从url提取参数之类的操作

它取决于s是什么-它是常数,它基于某种状态,它属于一个单独的包吗?

一种方法是将数据存储在全局变量中:

package main

import (
    "fmt"
    "net/http"
)

var replies map[string]string

func handler1(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    question := r.FormValue("question")
    var answer string
    var ok bool
    if answer, ok = replies[question]; !ok {
        answer = "I have no answer for this"
    }
    fmt.Fprintf(w, "Hi there, I love %s! My answer is: %s", question, answer)
}

func main() {
    //files := "bar"
    replies = map[string]string{
        "UK": "London",
        "FR": "Paris",
    }
    http.HandleFunc("/", handler1)
    http.ListenAndServe(":8080", nil)
}
为了简洁起见,我已经注释掉了文件,并将数据按原样放入地图中。你可以阅读文件并把它们放在那里

与卷曲配合使用:

$ curl -X POST 127.0.0.1:8080/ -d "question=FR"
Hi there, I love FR! My answer is: Paris

$ curl -X POST 127.0.0.1:8080/ -d "question=US"
Hi there, I love US! My answer is: I have no answer for this

我有点不清楚你想做什么,但是根据你说的,为什么不试着像这样封装你想传递的数据:

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

type FilesHandler struct {
    Files string
}

func (fh *FilesHandler) handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love %s!", fh.Files)
}

func main() {
    myFilesHandler := &FilesHandler{Files: "bar"}
    http.HandleFunc("/", myFilesHandler.handler)
    http.ListenAndServe(":8080", nil)
}

这为处理程序提供了更精细的控制。

我正在尝试创建一个字符串数组,该数组将从文件中读取,然后当post请求出现时,它将检查数组中的值。我需要将给定的数组传递给处理程序。如果可以用指针来做,那就太好了。你能给我一些例子说明如何做吗?我很喜欢这个答案,但我不知道如何将它转换成字符串数组,只要将
文件字符串更改为
文件[]字符串
并用数组实例化结构。如果要动态获取文件,只需添加另一个函数,如
func(fh*filehandler)GetFiles()[]字符串{//Do stuff以获取文件的字符串数组}
并在
处理程序
中调用它,如
fh.GetFiles()
好的,我做了这样的事情:您提到了一些关于修改数组的内容,我对此很感兴趣。我已经这样做了:但我对你将如何做感兴趣。请给我举个工作例子,因为我不懂。