Basics of Go

Cryptomasters Setup

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

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

The "Cryptomasters Setup" 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 walks through setting up the initial packages for a console application that utilizes the cex.go package to make API requests and retrieve data.


Transcript from the "Cryptomasters 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.

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

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.

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.

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.

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.

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.

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.

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.

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.

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.

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?

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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