From the course: Learning Go

Read a text file from the web - Go Tutorial

From the course: Learning Go

Read a text file from the web

- [Instructor] Go has a rich set of tools that let you work easily with web applications and services. The HTTP package lets you make requests and send data to remote hosts and also lets you create HTTP server applications that listen for and respond to requests. In this demonstration, I'll send a request for some data from a remote host. This starting main.go file has a constant named URL and I'll hold down the Command key on Mac or Control on Windows and click it and then click the open button on this dialog and that will open the results of this PHP file. So now I know I can successfully get to that page from my browser and my goal is to download that JSON content to my local development machine. First I'll need some new imports. The first one is called net/http and the other one is io/ioutil. Now I'm going to use a function of the HTTP package, named simply Get. There's also a post function, but I'll just be downloading some simple content. When I call this function, I'll get back a response object. I'll name it R-E-S-P for response and this is one of those functions that could also return an error so I'll add that and then I'll initialize those variables with http.Get and I'll pass in the URL constant. Next, I need to check my error. I'll say if error doesn't equal nil, then I'll call panic and pass in the error object. Now I want to find out exactly what I got back from the server. I'll call fmt.Printf, now pass in response type, and then a verb of uppercase T for the type and align feed and then I'll pass in the response object. I'll get rid of the call to print line and I'll run this version of the code. I'm getting a pointer to an object named response and notice that it's a member of the HTTP package. The response object has a public field simply called body and the body will represent everything I need, specifically the JSON packet. Now before I do anything else, I'm going to add code to close the body after everything else is done and so I'll use the differ key word and pass resp.body.close, because I'm deferring that call it'll happen after everything else is finished. Now I'm ready to read the content from the website. I'll be receiving a byte array and I'll name it bytes. Once again, I might receive an error from what I'm about to do and I'll call ioutil.ReadAll and I'll pass in resp.body. Next I once again need to check that error so I'll copy and paste this code and now I'm ready to work with that string. Now remember I'm starting with an array of bytes not a string, so I need to convert it. So I'm going to create a new variable that I'll call content and I'll set its value to bytes wrapped in the string function. And then I'll call fmt.Print and I'll pass in the content variable, and I'll run that version of the code and I successfully receive all the data from that webpage. So I've successfully received the data and now I can do something with it. So next I'll show you how to parse the data and turn a JSON formatted string into structured data that I can use in my application.

Contents