gpt4 book ai didi

Go:表示没有查询字符串的路径

转载 作者:IT王子 更新时间:2023-10-29 01:55:36 26 4
gpt4 key购买 nike

如何表示没有查询字符串的路径?

例如:

  • www.example.com/user 而不是
  • www.example.com/user?id=1

以下代码无效:

开始:

if r.URL.Path[4:] != "" {
//do something
}

最佳答案

func main() {
req, err := http.NewRequest("GET", "http://www.example.com/user?id=1", nil)
if err != nil {
log.Fatal(err)
}

// get host
fmt.Printf("%v\n", req.Host) // Output: www.example.com

// path without query string
fmt.Printf("%v\n", req.URL.Path) // Output: /user

// get query string value by key
fmt.Printf("%v\n", req.URL.Query().Get("id")) // Output: 1

// raw query string
fmt.Printf("%v\n", req.URL.RawQuery) // Output: id=1
}

play

关于Go:表示没有查询字符串的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26189523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com