Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of http in go language

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the http example analysis in the go language for everyone. Xiaobian thinks it is quite practical, so share it with you for reference. I hope you can gain something after reading this article.

Go Language http

1. http.ListenAndServe() method provided by net/http package, listening at specified address

This method is used to listen at the specified TCP network address addr and then invoke the server-side handler to process incoming connection requests. This method has two parameters: The first parameter addr is the listening address; the second parameter represents the server handler, which is usually empty, which means that the server calls http.DefaultServeMux for processing, and the service logic handler written by the server http.Handle() or http.HandleFunc() is injected into http.DefaultServeMux by default.

2. Processing https requests

func ListenAndServeTLS(addr string, certFile string, keyFile string, handler Handler) error

3. routing

The http.HandleFunc() method accepts two parameters

The first parameter is the target path of the HTTP request,"/hello," which can be a string or a regular expression in string form.

The second parameter specifies a specific callback method, such as helloHandler.

When our program is up and running, visit http://localhost:8080/hello and the program will call the business logic program in the helloHandler() method.

4.get/post access

resp, err := http.Get("... ")defer resp.Body.Close()body, err := ioutil.ReadAll(resp.Body)fmt.Println(string(body))resp, err:=http.Post("..... ", "application/x-www-form-urlencoded", strings.NewReader("..=... "))defer resp.Body.Close()body,err:=ioutil.ReadAll(resp.Body)fmt.Println(string(body))package mainimport ( "fmt" "io/ioutil" "net/http" "strings")func main() { //simulate a post submission request resp, err := http.Post("http://www.baidu.com", "application/x-www-form-urlencoded", strings.NewReader("id=1")) if err != nil { panic(err) } //Close connection defer resp.Body.Close() //Read everything in the message body, err := ioutil.ReadAll(resp.Body) //output content fmt.Println(string(body))}

Simulate an http server listening address: 127.0.0.1:8080

// http_server.gopackage mainimport ( //"fmt" "net/http")func main() { http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello world! ")) }) http.ListenAndServe("127.0.0.1:8080", nil)}

About "http example analysis in go language" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report