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

How to use HTTP package to create WEB Server in Go language

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you how to use the HTTP package to create a WEB server in the Go language. the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

There are roughly two ways to write a http web server in Golang:

1 use the net.Listen of the net package to listen on the port

2 use net/http package

Here is a discussion of how to create a web server using the net/http package

Net/http request provides the concrete implementation of HTTP client and server.

Http client

The first thing to see is the three functions of Get,Post,PostForm. These three functions directly implement the http client.

The copy code is as follows:

Import (

"fmt"

"net/http"

"io/ioutil"

)

Func main () {

Response,_: = http.Get ("http://www.baidu.com")

Defer response.Body.Close ()

Body,_: = ioutil.ReadAll (response.Body)

Fmt.Println (string (body))

}

In addition to using these three functions to establish a simple client, you can also use:

Http.Client and http.NewRequest to simulate the request

The copy code is as follows:

Package main

Import (

"net/http"

"io/ioutil"

"fmt"

)

Func main () {

Client: = & http.Client {}

Reqest, _: = http.NewRequest ("GET", "http://www.baidu.com", nil)

Reqest.Header.Set ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")

Reqest.Header.Set ("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3")

Reqest.Header.Set ("Accept-Encoding", "gzip,deflate,sdch")

Reqest.Header.Set ("Accept-Language", "zh-CN,zh;q=0.8")

Reqest.Header.Set ("Cache-Control", "max-age=0")

Reqest.Header.Set ("Connection", "keep-alive")

Response,_: = client.Do (reqest)

If response.StatusCode = = 200 {

Body, _: = ioutil.ReadAll (response.Body)

Bodystr: = string (body)

Fmt.Println (bodystr)

}

}

How to create a web server?

The http package is very bt, and only needs two lines!:

The copy code is as follows:

Package main

Import (

"net/http"

)

Func SayHello (w http.ResponseWriter, req * http.Request) {

W.Write ([] byte ("Hello"))

}

Func main () {

Http.HandleFunc ("/ hello", SayHello)

Http.ListenAndServe (": 8001" nil)

}

Listen to the port: http.ListenAndServe (": 8001", nil)

Register path handling function: http.HandleFunc ("/ hello", SayHello)

Processing function: func SayHello (w http.ResponseWriter, req * http.Request)

How efficient is the golang server?

It is true that the efficiency of the golang server is not as efficient as node.js, almost half of it. But then again, if some concurrency is not very large site, you can still use golang as a server.

These are all the contents of the article "how to create a WEB server with HTTP packages in the Go language". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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