Go-Package Intro : Go for Beginners

Go-Package Intro : Go for Beginners

In this article we will learn some basic about Go package directory. Here we will learn to create and use our own packages. We will also learn to install remote packages.

However, if you don’t know go’s basic convention and use of packages, please visit my previous post Go: For beginners before starting this one.

Package directory:

Assuming you know what packages for, we will start with creating a new package in the go workspace.

Every time you create a package, you should create a new folder in the src directory, with the notable exception of main, for which main folder creation is optional. Folder names are usually the same as the package that you are going to use. You can have multi-level directories if you want to. For example, if you create the directory %GOPATH%/src/github.com/ihm13/newpkg, then the package path would be github.com/ihm13/newpkg. The package name will be the last directory in your path, which is newpkg in this case.

Execute following commands: 
Cd %GOPATH%/src
Mkdir mymath
Cd mymath
touch sqrt.go

Type the following content to in sqrt.go:

// Source code of %GOPATH%/src/mymath/sqrt.go
package mymath
func Sqrt(x float64) float64 {
 z := 0.0
for i := 0; i < 1000; i++ {
 z -= (z*z - x) / (2 * x)
 }
 return z
 }

Now our package directory has been created and its code has been written.

Compile packages:

We’ve already created our package above, but how do we compile it for practical use? There are two ways to do this. 1. Switch your work path to the directory of your package and then execute the go install command. 2. Execute the above command except with a file name, like go install mymath.

After compiling, you can open the following folder.

cd %GOPATH%/pkg/%GOOS%_%GOARCH%    //%GOOS%_%GOARCH% = windows_amd64 for my machine
ls
// you can see a is generated 
mymath.a 

The file whose suffix is .a is the binary file of our package. How do we use it?

Obviously, we need to create a new application to use it. Create a new application package called mathapp .

cd %GOPATH%/src
mkdir mathapp
cd mathapp
touch main.go
Write the following content to main.go. 
//%GOPATH%/src/mathapp/main.go source code.
package main
import ( "mymath" "fmt" )
func main() {
 fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
}

To compile this application, you need to switch to the application directory, which in this case is %GOPATH%/src/mathapp, then execute the go install command. Now you should see an executable file called mathapp was generated in the directory %GOPATH%/bin/. To run this program, use the mathapp command.

You should see the following content in your terminal.

Hello, world. Sqrt(2) = 1.414213562373095

Install remote packages:

Go has a tool for installing remote packages, which is a command called go get. It supports most open source communities, including Github, Google Code, BitBucket, and Launchpad.

go get github.com/ihm13/newpkg`

You can use go get -u … to update your remote packages and it will automatically install all the dependent packages as well. This tool will use different version control tools for different open source platforms. For example, git for Github and hg for Google Code. Therefore, you have to install these version control tools before you use go get.

After executing the above commands, the directory structure should look like following.

%GOPATH%
  Src
    |-github.com
    |-ihm13
    |-newpkg 
  pkg 
    |--%GOOS%_%GOARCH% 
    |-github.com 
    |-ihm13 
    |-newpkg.a 

Actually, go get clones source code to the %GOPATH%/src of the local file system, then executes go install.

You can use remote packages in the same way that we use local packages.

import "github.com/ihm13/newpkg"

If you’ve followed all of the above steps, your directory structure should now look like the following.

bin/
  mathapp
pkg/
  %GOOS%_%GOARCH%/
    mymath.a 
    github.com/
        ihm13/ 
        newpkg.a
src/
  mathapp/
    main.go
  mymath/
    sqrt.go
  github.com/
    ihm13/
      newpkg/
        newpkg.go

Now you are able to see the directory structure clearly; bin contains executable files, pkg contains compiled files and src contains package source files. Hope you enjoyed so far. Explore my other articles on GoLang.

Reference:

  1. https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/index.html