Tag: HTTP

For a small project I am using Go, a free and open source programming language created at Google. I need to run multiple HTTP requests (APIs) concurrently. Go’s concurrency primitives (goroutines and channels) provide a simple and efficient means of expressing concurrent execution.
If the sentence “Concurrency is not parallelism” or “goroutines and channels” means nothing to you, I suggest you to watch this video: Rob Pike – ‘Concurrency Is Not Parallelism’ and read these resources: ‘How Goroutines Work’ and ‘concurrency in Go’.

So, assuming you are familiar with the concurrency/parallelism and goroutines/channel topics and that you have installed Go, we are going now to see ho to run concurrent HTTP requests.

I am using Go on a Windows 10 machine, go version go1.7.5 windows/amd64 and I am using Visual Studio Code IDE. If you want to use VS Code for Go (it works pretty well), check out the vscode-go extension.

I defined a struct called HTTPResponse with two fields:

status: the HTTP response status

body: the HTTP response body

1

2

3

4

typeHTTPResponsestruct{

status string

body[]byte

}

We use a channel to connect concurrent goroutines. The channel we are using is typed to HTTPResponse (the previously created struct). Given an array of HTTP urls (in the following example I am using an array of two urls) we invoke the function DoHTTPGet or DoHTTPPost in a goroutine (notice the go keyword before the call). This new goroutine will execute concurrently with the calling one.

The DoHTTPGet function makes the HTTP GET call to the given url and send the response (the struct HTTPResponse) back to the channel.

1

2

3

4

5

func DoHTTPGet(url string,ch chan<-HTTPResponse){

httpResponse,_:=http.Get(url)

httpBody,_:=ioutil.ReadAll(httpResponse.Body)

ch<-HTTPResponse{httpResponse.Status,httpBody}

}

The DoHTTPPost function makes the HTTP POST call to the given url and send the response (the struct HTTPResponse) back to the channel.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. If you want to know more or withdraw your consent to all or some of the cookies, please refer to the coockie policy. Got it!Reject.