Basics of Go

Create a Go Module

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
Basics of Go

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

The "Create a Go Module" 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 showcases the process of creating and running a module using the Go command-line interface (CLI). The discussion also covers the topic of creating multi-module applications using workspaces.

Preview
Close

Transcript from the "Create a Go Module" Lesson

[00:00:00]
>> How do you create the module? Well, let me get out of here for a second. And you create the module. I don't need to save this. Now, I'm going to open a folder. So we're going to create a folder for our project, okay? So just open a folder, create the folder somewhere.

[00:00:17]
Just remember what it is. In case you're in Windows, try not to use desktop, because sometimes you may have permissions, Go cannot execute, blah, blah, blah, you need to change permissions. So just use something outside of the users directory, just that. But I'm going to just create, I have a trainings Frontend Masters here.

[00:00:41]
Going to create a folder here for Go workshop. And within this folder, I'm going to create another one for this project. So we're going to start working with a basic project. So let's work with InputOutput, we're gonna call it InputOutput, okay? Just pick any name, it's just a folder.

[00:01:01]
Now, I'm going to open a terminal on that folder. And if you're using VS Code, the advantage of opening a folder is that now the terminal is pointing to that folder, which is pretty good. And now we're going to use the Go CLI to create the module. Again, what's the module?

[00:01:18]
Your project. So it's go module, go with space module, a space. I need to just press Enter. It says, no, I don't know what module is, because they say, it's better to just use mod instead of module, okay? So we are going to use mod and not module, for mod, just remember that.

[00:01:42]
And later, there is something known as workspace, and they use work, not workspace as the CLI tool. So it's go mod, and then a command over the module, so go mod. And we have init to initialize one module. If you try that, it's also going to give you an error, because it says, you also need to give me the name of your module, the name of your project.

[00:02:10]
And it's suggesting, and it's not mandatory, to use a URL system. Again, the URL doesn't need to really exist on the network. So that means I can say go mod init, and then, I don't know, frontendmasters.com/go/io, InputOutput. What happens if you wanna change that later? You will see, it's pretty simple.

[00:02:39]
Because if I press Return now, the only change that it did is that now we have a file in our folder that wasn't there before. It's a go.mod, a module. And if you click on that file and you open that file, you will see all the metadata for our project, that is just this.

[00:02:59]
So how do you create the module? You just need one text file with the name go.mod in the root folder of your project, that will say module, space, the string you want. If you wanna change it, you change it. And then the version of Go that you wanna use.

[00:03:19]
I mean, you can also say, I wanna use 15, and that's all. So it's actually completely simple. Remember, we are in a simple language. And sometimes we will see we are going to be following a lot of rules. For example, the name of this file is part of the rule, so it must be go.mod with that content.

[00:03:39]
You don't like the name we set, you change it from here. That's all, okay? So now we have a module, okay? Because we have a module, what's the difference? That now when we are running an app, to run an app, it was go run, remember, and then the name of the Go file, that I don't have any yet.

[00:03:59]
But the idea is that because we have a module now, instead of specifying the file that I need, I can say I wanna run this folder, this module, go run ., that will say, I wanna run this module. And in this case, it's going to look through all the files in the module, looking for a package main with a function main.

[00:04:23]
Does it makes sense? That I don't have right now, so nothing will happen. So that's when it says, you don't have any Go files, okay, which is fine. But now, I don't need to say the name of the Go file that I wanna run. So I just need one anywhere in that folder with any name that inside has package main with a function main.

[00:04:45]
Typically, we call it main.go, but you don't need to. You wanna call it home.go, I don't know, index.go, it's up to you. The only important part is not the name, but it should start with package main, and it should contain a function with the name main. That's the only thing we need.

[00:05:08]
So now if I print again, Hello from a module, semicolon, no, we don't need that. If I save it and then I execute again, go run ., it will say, Hello from a module, okay, very simple. First, a couple of things that I didn't mention. Can I change where I put, for example, the opening curly brace like that?

[00:05:36]
Well, you can see immediately that the answer is no, because I have a problem now, unexpected semicolon or newline. So you cannot start with a code block, so that when you open a code block, it'll be on the same line. Remember we don't have freedom here. So, I mean, if you're coming from Javascript, it's okay, it's okay.

[00:05:57]
If you're coming from C++ or other languages, or even C#, yeah, maybe you don't like that opening curly brace there, but that's how it will look like in Go, so get used to that. Okay, cool, so now we have a module. Typically, you start a project with a module.

[00:06:17]
Again, what's a module? For now it's a folder, okay, with a go.mod and main.go. If you want, for those of you coming from Node.js, the go.mod is kind of a package.json, kind of. In fact, if later you will use external dependencies, those dependencies will be listed here in this file as well.

[00:06:40]
Cool, well, let's go back to the slides. And let me just mention that lately in Go 1.19, so the previous last version, so in the past six months from recording this course, they added an idea known as workspaces. Actually, I need to also give you a warning. In the first version of Go, we used to have something known as a workspace, okay, that then they deprecated that, I think it was around 1.10, and they move into the modules concept that we saw so far.

[00:07:15]
But now, they said, there are a lot of developers saying, we need another layer in the structure, because sometimes we wanna separate our project in different modules. And then they created, again, the idea of a workspace that isn't kind of a super module that is on top of that.

[00:07:35]
So that's new, okay, 1.18, not 19. And instead of having a go.mod, you have a go.work, which's also metadata, okay, and then you use go.work in it. There is no need to use that now, and most of the Go apps out there are not multi-modules so far, so they don't have a workspace.

[00:07:59]
But anyway, it's also pretty simple, it's another folder structure. So you have a workspace, and within the workspace, you can have several folders, each folder will contain a module, and each module will contain a package that we haven't created one yet, okay? So that's the structure, workspace, modules, and packages.

[00:08:17]
And workspace is optional these days, and I think it's going to be optional forever. So you're gonna stay with module and packages.

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