O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

Connecting to the HTTP server can be done with help of the net/http package. Naturally, there are more ways you can achieve this, but the code above illustrates two of the most common approaches. The first option implemented the simplePost function, and illustrates the use of a default client. The POST method is chosen here as it is more complex than GET. The Post method accepts the URL, content type, and body in the form of Reader. Invoking the Post function immediately requests the server and returns the result.

Note that the Post method is just wrapping a function that uses the http.DefaultClient in its implementation. The net/http package also contains the Get function.

The useRequest function implements the same functionality, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required