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

The "Making an HTTP Request" 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 the usage of the strings package to perform basic operations on UTF-8 encoded strings, such as manipulation and transformation. It's important to note that these functions generate a new copy of the string and do not modify the original.

Preview
Close

Transcript from the "Making an HTTP Request" Lesson

[00:00:00]
>> If your returns, you will see it's adding me the package, it's important net http, that's from the standard library. And then I can start executing some codes, some libraries, some functions from it. For example, we can start working with Get. What is Get? It's actually making an http request with the Get verb.

[00:00:23]
You know that http includes different verbs, Get, Post, Put, Delete. Well, all of them are actually available as methods here. Actually is that a method? Was it correct when I said it's a method? What do you think? So if I wanna make a post HTTP request, is that a method of HTTP?

[00:00:48]
Mm-hm, interesting, right? The answer is no, it's not a method. Why? Because HTTP is not a structure, HTTP is the package. And post it's actually a function, an exported function in that package. I can actually Control-click or Command-click on the Mac and get into that, okay? And you will see how it looks like.

[00:01:11]
And you will see it has no, a thing like this after the func. So it's not a method, it's a function. Does it make sense? I mean it looks like a method, it looks up because when you are importing a package it looks like an object and then you are executing methods of that object, but not the reality in go, okay?

[00:01:32]
It's just a function in that package. So we have Post, Get, Delete, and also we have a more generic one called Request. In that case, you can specify the verb as an argument, so basically Get and Post are shotcalling request. This is in case you need to go deeper and maybe create your own verb or something like that, that is technically possible.

[00:01:56]
But anyway, typically in this case we're going to get something, okay? So we're going to get something from the network, so we will call Get. So if we analyze the signature of that function it receives a URL as a string, so pretty simple, straightforward. We don't need to create the URL object or parse it or just the string and then we receive a response, it has a star there.

[00:02:21]
Remember what's, what's that? It's a pointer. We'll see a pointer, we'll see why is that, and then an error. So we had that design pattern of object or error, so we know how that works. So I will pass the string first, I will see how to get the string in a second.

[00:02:37]
And I know that I will need to receive two things. So, for example, there is the response and an error object. I create two variables with this, column equals is creating a variable, in this case two variables from there. So, we need to know the URL, so I will show you how the URL looks like, it's not a big deal.

[00:03:02]
So I'm going to create a constant for that, let's say apiurl, do I need to use capital letters? It depends if you don't want to expose that URL to other packages, you don't need to. It's going to be then private to my package only. This is https, colon forward slash forward slash cx.io, forward slash api, forward slash, ticker.

[00:03:29]
And then the idea is, this how this work. The idea is we need to pass the two currencies uppercased with the three letters ISO code. For example, if I want bitcoin, it's BTC to USD. If you want bitcoin to Euro, use EUR, GBP, Pound, British Pound and so on.

[00:03:54]
So the problem is that I want my app to support different currencies, at least. Let's say that we compare that everything to USD, you can change that to Euros, Pounds or gents or whatever. But that one is going to be dynamic, right? How can I make it constant?

[00:04:10]
That then my change, kind of weird, right? Because if it's a constant that cannot change that you can say, well, let's create the variable. But anyway, the pattern typically is to use a placeholder here that we can use later with Sprintf. So we can say, you know what, here I will add a string.

[00:04:35]
So we use that constant as actually a pattern for a formatter, okay? Yeah, pretty simple. So then we can here call format or fmt, Sprintf. Remember the s prefixed version returns a string is not printing to the console or to the standard output. And then it will use that format, the apiurl, and the only variable that I have to change is actually the currency that I receive as an argument.

[00:05:11]
Okay, makes sense? The only thing that I have to be sure is that the currency is uppercased. So yeah, we can request that to the consumer of the API or we can start thinking about, do we have any string management? So can we convert that into like an opera string?

[00:05:32]
So first, should I try with dot and see what do we have here? No, why? Because strings are not objects. Actually, nothing is an object here, remember, but it's not a structure either. I could add a method, or I could create a structure that has a type. I can create a URL type or something like that and then add a method to it.

[00:05:58]
So to play with strings we actually always need to use global, let's say global functions. So within the standard go system, we have len for the length of the string. And if we want to go up to uppercase, we actually have one particular package in the standard library that will help us, okay?

[00:06:21]
And it's called strings. So strings is a package with a lot of functions, useful functions for working with strings. We can compare, we can check if a string contains another string. We have a count, you say, well, what's the difference? Also we have other methods that are better than len because I can count len, okay?

[00:06:48]
But we can get, I'm not going to get too deep here, but just to give you an idea, len is going to actually give you the length of that string in bytes, okay? Which he will just say, okay, it's using UTF eight by default, so, for most characters is one character, one byte.

[00:07:07]
However, there are a lot of characters, for example, flags on Unicode emojis, that they're actually using more bytes. So when you get the length, maybe you have one emoji, but the length will say four. So, that's why you have functions to actually get how many characters do I have for the UTF standard not how many bytes.

[00:07:30]
Okay, I'm not sure if you know this but if I use a flag, okay, that flag actually is a couple of bytes. In fact VS code, I'm not sure if you try that, VS Code has, I think it's a bug, but anyway. When you're using emojis in VS Code, for example, that flag.

[00:07:46]
Now, if I press the back key, look what happens. I see now a U, I don't know, what's that? Actually, the flag has, first, I'm simplifying, but the first byte says that it's a special emoji. The second byte says it's a flag and the other two bytes is actually the country code, US.

[00:08:08]
So when you are pressing the back key, it's just deleting partially that character and then you see like another emoji, another UTF character. You can see that with, for example, families, this is going to be a little weird. So for example, there is an emoji for a family, okay?

[00:08:25]
Not sure if you, let me increase the font size a lot, like that. And then if you start deleting, you press the backspace, you're starting deleting,
>> [LAUGH]
>> Members of the family, okay? I already said it's weird, okay? So you have a family and then you has start pressing the backspace, okay?

[00:08:44]
And now we are removing,
>> [LAUGH]
>> And I don't want to say why this happens but the last one there is dad, okay? The man is the last one standing there in the emoji. Anyway, so, but that's actually just proved that emojis, okay? They are using more bytes, and when you get the length, you get, in this case, four or five because now you can get families and then attach to the family skin color or hair or gender.

[00:09:17]
It can be non-binary. Well, each characteristic that you're adding to the emoji is another byte, okay? So have in mind when you are using len for the string, you will get bytes not characters. But you do if you need, so actually, I mean, if you're making a Twitter clone now that you don't like the new Elon Musk Twitter, you are creating a clone.

[00:09:39]
And you are measuring characters, maybe you should use the strings manipulation functions to get the amount of real characters used there, okay? Well, in the strings, we have ToUpper, ToLower. We can split, we can join, so most of the string-based functions available, are available here in the string.

[00:10:03]
So I can actually say ToUpper, but look at this. It's not a method of my string, ToUpper will actually receive one string, and it will return another string, okay? So I actually need to pass one string and then save it in another variable, or directly pass it as an argument.

[00:10:24]
That is going to be in this case the upper currency and then I can use upCurrency now, something like that, okay? So that's how you use strings functions in Go. You use the strings package and then you call the function. But have in mind it's not modifying the original string, it's returning you another one, a new one.

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