Significance of Go — Part 2 (Play with VS Code and Introducing Go packages and modules)
In the previous article, we identified the significance of Go, and where it stands among other programming languages. Here, we will learn how to set up the development environment on your local machine and an introduction to Go packages and modules.
Prerequisites
- Download and install Go by choosing the specific platform that you are working on. Here I will be using Linux (Ubuntu 18.04.4 LTS).
- Install Visual Studio Code
Setting up VS Code
Open up VS Code and navigate into the packages section as shown below.
Here, you can see I have already installed Go extension from Microsoft. Go ahead and install it on your editor too.
After that, press Ctrl+Shift+P and open up the command window in VS Code. In the search bar type “Go: Install/Update Tools” and select it.
When you select that you will be asked to select the tools as below. Select all of them by ticking the checkbox in the left-hand side of the search bar and click OK.
An installation process will be started in the VS Code terminal and it will take a few minutes. After the installation, it will show you a message as shown below. (Remember by time to time, if your installed Go tools behave weirdly better to do the installation, again and again, to keep them updated.)
Now, to check whether the tools have been installed correctly, let’s do a small exercise. First, create a new file named “main.go”, anywhere in your machine (I have created this in the Desktop directory) and copy-paste the code below. (Note that below code does not have an imported statement)
package mainfunc main() {
fmt.Println("Hello, playground")
}
After copy-pasting the code, press Ctrl+S to save it. You can see that VS Code will now automatically adds the import statement to your code as shown below. (Also note the different colours for different keywords)
Just try removing the line 8 from your code and save it, then automatically the import statement will get removed. This shows that our tools have been successfully installed.
To run the code, just navigate to the location where you have saved your file (in my case it is Desktop directory) and type the below command.
go run main.go
You can see that the program has been run and printed the output. Note that, when running the application, Go does not explicitly create an executable file or a temp file. It directly compiles and executes the application without hassle.
Introducing Go packages and modules
Before moving to modules, we need to have a brief idea of what Go packages mean.
Go programs are constructed by linking together packages. A package in turn is constructed from one or more source files that together declare constants, types, variables and functions belonging to the package and which are accessible in all files of the same package. Those elements may be exported and used in another package.
In our previous example, we created a package named “main” which consists of the main function. It is an example of a package. Also, we imported “fmt” to call the Println(…) function. Here, “fmt” is also a package.
Go module can be defined as a collection of Go packages. We define a file named “go.mod” which consists of the details about the dependencies, in other words, which lists down the details of the other modules that are needed to build your module successfully.
Creating a module
In the previous section, I created a file named “main.go” in Desktop. Create a new folder named “mygocode” in Desktop and move the “main.go” file inside it. Open up the folder in VS Code.
Navigate to the “mygocode” directory, open up a terminal and type the below code.
go mod init example.com/myfirstprogram
This will create a new file named “go.mod” and it will look like as follows.
Here “example.com/myfirstprogram” is the name of the module and “go 1.14” is the Go version which I am using.
Previously we have used “go run main.go” to run the particular code in main.go file. Since we now have a module, irrespective of where we are in the project directory, we can use the below command to run the module.
go run example.com/myfirstprogram
Just change your code as below.
package mainimport (
"fmt""rsc.io/quote"
)func main() {
fmt.Println("Hello, playground")
greeting := hello()
fmt.Println(greeting)
}func hello() string {
return quote.Hello()
}
Now check “go.mod” file. It will look like below.
module example.com/myfirstprogramgo 1.14require rsc.io/quote v1.5.2
The go command resolves imports by using the specific dependency module versions listed in go.mod. When it encounters an import of a package not provided by any module in go.mod, the Go command automatically looks up the module containing that package and adds it to go.mod, using the latest version.
Let us create another repository named models inside our project repository. Inside the models repository, create a file named “book.go” with the following code segment. (Note that, when you are typing the word “package”, VS Code will automatically suggest you “models” if you have installed all the plugins successfully)
package modelstype Book struct {
Title string
Sold bool
Pages int
}
Now just change the code in the “main.go” file by adding the below code segments inside the main() function.
func main() {
b := models.Book{
Title: "Harry Potter",
Sold: true,
Pages: 900,
}
fmt.Println(b)
}
The final code should look like below.
Note that, when you are typing the above code segment, the package model will get automatically imported in the import section. Also, as you can see there is a new line space between “fmt” and “example.com/myfirstprogram/models”. The reason for that is, the code is auto formatted to separate the standard packages and the packages that we created.
You can run the program either using “go run example.com/myfirstprogram” or “go run .” as shown below.
Also you can build an executable by typing the below command.
go build
The built executable can be run as below.
To learn more about Go modules please refer below link.
Conclusion
In this article, we have discussed the way to install the tools in VS Code to support Go. Also, I have briefly introduced what Go packages and modules are.
Meanwhile, try to find the answer to the question below until we meet next time.
Who is a “gopher”?
Previous Article
📝 Save this story in Journal.
👩💻 Wake up every Sunday morning to the week’s most noteworthy stories in Tech waiting in your inbox. Read the Noteworthy in Tech newsletter.