Coding Interview Coding Question 76: HTTP Get in Golang

Описание к видео Coding Interview Coding Question 76: HTTP Get in Golang

Today's DevOps interview question is a coding question in Golang. You need to write code that performs an HTTP GET request to a known URL like example.com, test.com, google.com, or espn.com.

First, start with a package declaration in Golang. Then, define the packages to import: fmt for printing, net/http for HTTP functions, and io for handling streams. Next, write the main function.

Here's a breakdown of the code:

Use http.Get to perform the GET request, returning a response and error.
If there's an error, print a message and exit.
Defer closing the response body to free resources.
Read the response body using io.ReadAll.
If there's an error reading the body, print a message and exit.
Convert the byte stream to a string and print it.
Running the code with http://www.espn.com returns the ESPN page's HTML. Changing the URL to example.com returns a shorter response.

If you don't convert the bytes to a string, you'll see a bunch of byte values instead. Removing the conversion will show these byte values when the code is run.

I hope you found this useful. If you did, please like and subscribe, and I'll see you in the next video.

Комментарии

Информация по комментариям в разработке