Basics of Go

Printing The API Response

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

Check out a free preview of the full Basics of Go course

The "Printing The API Response" Lesson is part of the full, Basics of Go course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates how to make an API call from the main package and showcases the format of the returned data.


Transcript from the "Printing The API Response" Lesson

>> We were playing brave here, because we haven't tested this. But now, we can go to main.go, that is still completely empty and try to use this. So what do I wanna try first? Well, I will try to go and call my api. So what's the name of the package, api?

Should I use cex? I don't know if it's cex or cex. Cex sounds complicated. Anyway, for cryptocurrencies at least, but anyway, cex, let's say. Should I start writing cex.?
>> api.
>> api., that's the name of the package, correct. And then I have the GetRate. It's going to import API, and then I can parse on BTC and try to receive BTC, it's Bitcoin.

So I can try to receive the answer that is going to be a rate or a possible error. And for that, I will just bring both. It was just a quick print for debugging purposes. I'm not going to use this as the final version. So let's try this, so go run..

First, look at this. This is Lulu. Who is Lulu? Lulu is a firewall that I have to on my Mac that I'm going to disable right now you will see why. Because it's detecting a new app in my Mac trying to get to the network, and let's say it's okay, this is new.

We won't allow that. I can say Allow, okay? That means that something's happening, it's going into the network, okay, and something will happen also. There is a timeout. So at one point, if the firewall is, and there we have it. We have something that we are receiving. Probably, I don't know how to read that, okay?

That's probably my print that I have here. So it went to the network and we received something. If I try again, If Lulu is again giving me an alert, I say Allow, okay? Now, I try again. LuLu gave me an alert. Why? Lulu, my firewall is giving me, I'm saying allow, and that should stay forever.

And did I change the rule? Why is Lulu giving me the same alert on every time I run in the app? Does anyone know why? Go is a binary, and actually we are running. But if you remember, running is not actually like running the same app. It's building a new app every time in a temporary directory that I'm not seeing and running it.

Every time I'm executing run, it's building a new app. So from an OS point of view, it's a new app that is trying to get to the network, okay? And also now, we got the JSON. Why the JSON wasn't there the first time, it was because Lulu was blocking it for a while and then it goes to a timeout.

But we see the JSON as a string, so it's working. And what we see here, the the the rate and the error. And yeah, that they're pretty bad, okay, because the rate is actually a memory address. It's a pointer, so we will need to do something better with that, okay?

But in fact, I just tried to show the price of the rate and I will explain something interesting. I will delete Lulu from memory for now, okay, and this is my output, my rate. It's not probably the notation that I expect, okay, but we'll make that better. So what's going on here?

Rate, what's the type of rate? So if I write right here, what's the type? It's not my structure. It's a pointer to my structure, okay? But then I used dot and it worked, so I didn't need to do something like this, get into the object and then called price or something like that, okay?

Well, that is because the dot syntax, when the dot operator actually works with pointers and it goes to the object, it goes to the instance to make our life easier. So if we have the structure instance or a pointer to the structure instance, how you call methods or how you access properties is the same, just the dot, it's like a shortcut, okay?

Make sense? Cool, so, so far I have my code working in terms of I'm receiving a JSON from the server, but we need one more step, right? So we need to parse the JSON, because right now, it's just a string.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now