Transcript from the "Crytpomasters Setup" Lesson
>> So I just need to create a folder. So let me get into the right folder. We are going to call this project, let's call it crypto masters. We're going to get the current price or rate from cryptocurrencies, from a web service. So we are going to do crypto masters, by the way, this is going to be also still a console application, and then we are going to get into creating a server.
[00:00:29] So this is going to be how to consume data from the web, and then we are going to see how to serve that data to the web. So we create the folder the next step is always to create the module. So we open the terminal, go mod init and the name of the project that we can make a URL based name such as frontendmasters.com/go/crypto.
[00:00:56] Remember, this is just creating a go.mod file in the root, and typically the next step is to create a main.go file. Remember, the name of the file doesn't matter, it can be any name. The important part is to have the package main and a function with the name main.
[00:01:13] That's the only required step here. So we will use a simple web service. There are a lot of web services to get data on cryptocurrencies, but we're going to use one from cex, c-e-x, that has the advantage that doesn't need any key. So we can just point to the URL and we get the JSON.
[00:01:34] So it's pretty simple. But if not, there are a lot of APIs out there, but you need to probably sign up to get a key and then use the key as part of your request. So to keep things simple, we are going to use that one. But first we will start thinking about the structure that we are going to use.
[00:01:51] So we're going to create the data structure. But actually, you're going to start pretty simple, so it's not going to be a big deal. So I'm going to create a new structure that we can call this currency, or rate, or something like that. I can do that in the main package but we know that typically, it's not a good idea to work with the main package unless it's a very simple script, and you know what you're doing.
[00:02:11] But most of the time we want to package. The package, remember, it's just a folder with a name. We can call these currency, or rate, or data, data types, model. In this case, it's up to you. We don't have one way of naming things here, so it's wherever you like, let's say data types.
[00:02:30] And within that package, I will create a file. Remember that file names don't matter here. So I can just say that here I'm going to write the cryptocurrency or maybe say data.go. The name doesn't matter. The only important part is the package declaration. That's the only important part here.
[00:02:52] And then I can create here my structure. I'm going to create a new type. I can call it rate, I can call it crypto rate, currency rate, it's your name, but we know now, we are confident that we need to start with a capital letter, okay? So we know that, that's clear.
[00:03:09] So, let's call that rate, and it's going to be instruct that we'll have for now just the Currency, capital C, because we know that properties, if we want those properties to be accessed from the outside, we will need a capital letter. That's gonna be just a string, the code of the currency, and the price.
[00:03:32] Remember that when we're working with prices, we can use float 32 or 64 based on the precision that we want. We're talking about cryptocurrency, we have a lot of decimals. It depends on the currency, but maybe it's a good idea to use float64. But anyway, even if you're working with prices in US dollars or euros, that you have only two decimals, float32 will be good enough.
[00:04:00] However, I mentioned that when you are using the JSON encoder and decoding tool from Go, it will take numbers as float 64 by default. So if you set your data float 32, it will be more efficient in terms of memory management. But then you will need a conversion step every time you are reading from or creating JSON, if you're reading from JSON or create in JSON, you will need to convert those float 64 to 32, okay?
[00:04:31] That is not a big deal but you have in mind that. So that's why most of the time we shall say unless you have a very specific requirements on performance and memory management, we use float 64 most of the time. Okay, that's all. So we don't need anything else here.
[00:04:46] So actually it's pretty simple. So now I'm going to create a new package. So this is for data types, we can leave it like that. I'm going to create a new package to another folder and we can call this the API. So the folder should be in the root.
[00:05:05] So we're going to here store the API related functions, types, and variables. New file there and we can call this cex.go because that's the name of the provider we're going to use. And here we are going to get then some information from the API. So the first package, API and then I'm going to create an export that function because it's going to be exported.
[00:05:35] It will start with capital letter. So it can be get rate, for example, that we will receive a currency as a string. And it will return something, we will see then what we are going to return. We should return a right option. So the one that we have just created.
[00:05:54] So an instance of the structure that we have just created. So I'm going to refactor this in a couple of minutes, so it's okay if you wanna follow me, but I have in mind that I will mix a little change later, that of course you can do it also.
[00:06:08] First, if I say Rate, it doesn't work. Does anyone know why? Yep.
>> Not in the same package.
>> It's not in the same package. So Rate it's in a different folder, different package, so I have to import that. And I mean, I don't need to import it manually, at least what I need is to start writing the prefix of that.
[00:06:29] So it could be data type, data types. If you press Return and Save, it will add the import for you. But have in mind that we need to import the package. Remember that we are importing the package, not individual elements or individual names. I'm not importing rate. I'm importing everything from the package.
[00:06:51] Everything that was exported on that package is available here. So if you like to have control, more control on what you are importing, maybe you need more packages. So you need to change how you're thinking about packages. Maybe you can create really small packages just for niche features of your app.
[00:07:10] So don't use packages as you're used to on other languages because there you have other abilities to control visibility. But here, the only thing we have is package and sport that are non sport and that's all. Okay, so here what we need to them is that we need to use the http system, so we need to make an http request.
[00:07:34] Fortunately for us, Go has http directly built in in the language, in the standard library, so we don't need an additional library, it is part of Go. We cannot build a Go application that has no http library access. It's there, by default. And for that we have an http package.
[00:07:55] By the way, it's http, but that also means http with SSL. So HTTPS as well. We don't have a different one for https, it's the same, okay, http.