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

Setting up VS Code

Package Section in VS Code

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.

Open up the command window and search

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.

Tick 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.)

After the installation

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)

Automatically adds the import statement to your code

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
Running the saved file

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

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

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.

Initialized go.mod file

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
Running the module

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
}
Newly created file under models directory

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.

Modified main function

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.

Run the program

Also you can build an executable by typing the below command.

go build

The built executable can be run as below.

Build and run an executable

To learn more about Go modules please refer below link.

Conclusion

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.