Familiar with GO!

Mahedi Hasan Jisan
4 min readOct 5, 2021

“Go is an open-source programming language that makes it easy to build simple, reliable, and efficient software.”

Sometimes it’s required to import packages from another module in your code. In the Go programming language, the Go.mod handles that part, which looks for those packages from the destination module. Go.mod stays in your codebase along with the source code. In order to create the Go.mod file, run:

Command: go mod init <name>

In a typical situation, a module path should be a repository location where the source code is located such as github.com/mymodule . Let’s create a hello world using go language!

Looks like c/c++? Hmm, Interesting!!

To get help with Go language, type:

Command: go help

So far we have talked about what is Go programming language? similar to c/c++! We have also created a hello world example. Now, let’s verify how the Go.mod actually works. You can find the existing module from this location: https://pkg.go.dev/

For the sake of an example, we are going to use a package from one of the existing modules. The selected module is quote -> rsc.io/quote -> func Hello() string!

Your updated hello.go would be like this:

Now, you have added the package, which is “rsc.io/quote” to use the hello function/module. However, there is an error, which says you did not set the package to use that module. That is where the Go.mod comes in as we mentioned earlier. Now, to common sense, the module you just imported, requires a package right? Yes, it is and the fun thing is you don't have to do it manually. All you have to do is execute the following,

Command: go mod tidy

This command will try to sync the “hello.go” file with the “go.mod” file. It will find out the dependencies to use the rsc.io/quote module and set it inside “go.mod” file. Go ahead, try it out! You will see something like this:

go mod tidy!

Now, run the program again using: go run . You will see hello world in the terminal!

Go code is grouped into packages, and packages are grouped into modules. Your module specifies dependencies needed to run your code, including the Go version and the set of other modules it requires.

Creat Go Module?

Step 1: Create a directory and create your module using: go mod init <command>

Step 2: Create a file called “sum.go” and write a code that will sum up two numbers.

Step 3: Call your code from another module! follow back steps 1 and 2 to create another module so that you can call the sum() from there.

Step 4: Now, your module is not uploaded in any location. In that case, you have to adapt the situation so that your go module can find it in your local machine. You can do that by using: go mod edit <command>

After the go mod edit <command> command, run the go mod tidy, which will update the dependency requirements in the “find/go.mod” module.

Step 5: Everything is set, just simply run the following command: go run .

Voila!

You just created your own module and used it from another module using the Go programming language.

Next Steps:

Compile and install the application?

  • The go build <command> compiles the packages, along with their dependencies, but it doesn’t install the results.

That is a quick overview of how the Go programming language works! You can try the rest by following this tutorial: https://tour.golang.org/welcome/1

Cheers!

--

--