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

The "Go Basics Overview" 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 discusses fundamental syntax rules of Go, emphasizing the use of curly braces to contain code blocks, the case-sensitivity of the language, and its non-object-oriented nature without classes. The discussion further covers essential rules governing the contents of folders and files in Go.

Preview
Close

Transcript from the "Go Basics Overview" Lesson

[00:00:00]
>> Let's talk a little bit about language types to understand where Go is right now. So we have different kind of languages. We have languages where developers are writing code. And then we are shipping source code, such as JavaScript, Python, okay, Ruby maybe. We have some languages where we actually go into compile code, but not into the binary final code for one specific architecture, but in an intermediate language.

[00:00:34]
And typically, we ship bytecode. So we deploy bytecode, and that's, for example, Java or C#. So we compile. We don't deploy .java files, we deploy .class files typically grouped in a container. And then we have compiled languages that are actually creating directly machine codes. So a code that executes directly in one CPU within the context of one operating system.

[00:01:05]
And here we have Go. By the way, that's the mascot, okay, of Go. So Go is here, it's a compiled language. However, we already mentioned that also Go can ship source code as in transpiling into JavaScript. They can also ship bytecode, for example, into WebAssembly, that is, WebAssembly the format, the WebAssembly container is kind of a bytecode and also machine code.

[00:01:34]
That is what we will do here today, okay? But we are there, so we don't compare go with Python in terms of the nature of the language. We compare that more with C, okay? So fundamentals of the Go syntax. Basic rules, we use .go files. We saw that in action.

[00:01:59]
We use code blocks, curly braces. So we don't care as in Python about spaces or tabs that we have. We don't care about indentation, it's just about curly braces. We don't have a styling freedom, and you didn't see that yet, but you will in 15 minutes. We do have semi-colons to separate sentences, but it's optional.

[00:02:24]
And the best practice is not to use them unless, for some reason, we are defining two sentences in the same line, okay? So if you have two prints in the same line, well, you must separate those print calls with a semi-colon, but we are not going to see semi-colons a lot, yeah.

[00:02:45]
>> Does that mean it reads new line as a delimiter?
>> Yeah, so a new line is a delimiter, but it's not always true because, let me show you something, and that's a spoiler. I can do this. And you can see it's giving me an error, okay, say, let me open from here.

[00:03:09]
What's going on here? What is that? Have a lot of errors. That's first one to go, this one. A string literal not terminated, but I can make it work pretty easily, and also, I can print more stuff and go to a new line. And on some situations, well, we didn't create variables yet, so I don't wanna show you that.

[00:03:37]
So I mean, what I wanna say here is that it's not like every time you have a new line, it's a new sentence. So the compiler will have its own rules, and most of the time, it works as expected, okay? And by the way, the same happens in JavaScript.

[00:03:52]
In JavaScript, semicolons are optional, but typically, this is changing right now, but typically, the design guideline is to use it, okay? But maybe that's changing, well, not here. So in Go, and that's also applicable to Swift and Kotlin, we don't use semicolon unless we need that. For example, if I have two print declarations like this, this is not gonna work because it says, no, you need to go to a new line or you use semicolon, okay?

[00:04:30]
But it's for some special cases. We are not going to use them. If you are used to that, yeah, you need to train your brain, okay? Use the machine learning capability that you have in your brain to train it to not use semicolons. Okay, so we already said that it's optional.

[00:04:50]
So we do have them, but it's optional. It's a case-sensitive language. So a name variable with capital N or with lowercase n is not the same variable, okay? It's case-sensitive. It's strongly typed, so which means if you're coming from Python or JavaScript, you need to get used to work with types.

[00:05:13]
So every variable will have a type and we can't change that type dynamically. We will see later how we can do some dynamic things because sometimes we have some use cases. But first, let's start, this is a strongly typed language. And it's not an object-oriented language. We don't create classes.

[00:05:37]
We don't create objects. So no classes, also we don't have exceptions. Try-catch, no, they don't exist here. We don't have something known as an exception. We do have a way to throw a fatal error. We will see how to do that. And also we have a design pattern to work with errors, and we will see how that works as well.

[00:06:05]
But that's not what you know as exceptions. We don't have try-catch. It's going to be something different. Again, if you're asking, why, why? Remember there are three goals, that's why. Adding that will probably, in this case, affect execution performance. So we have typically one file acting as the entry point.

[00:06:30]
That's the one that we need so far. It has a main function. Then we're going to start adding more stuff, for example, what's a package? A package is a folder, okay? We will start working with that in a minute. Packages can have simple names such as, services, utilities, data, or URLs.

[00:06:53]
So you can actually use a URL as a package name, but doesn't need to actually match a real URL. And within one Go file, so in .go file we can have variables, functions, so far we did only one function, type declarations, we haven't seen one yet, and methods declaration.

[00:07:19]
We will cover all of this, okay? But we will have Go files with variable functions, type declarations, and methods declaration. There is a constant, well, that typically goes under the variables umbrella. And then we have modules. What's a module? Roughly and quickly, a quick and dirty definition, a project.

[00:07:42]
So far, we have just created only one file without a module. So it wasn't actually a Go application, it was a standalone script. That is not common, okay? So typically, you don't do that. So you create a module.

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