Getting started with Go programming – Import

So far we used standard packages that comes as part of the golang such as fmt, strings etc. Now we will see two things one how to use a third party library and how to create our own package.

An important variables which will be useful for working with packages. The GOPATH environment variable is used to specify directories outside of $GOROOT that contain the source for Go projects and their binaries. For more information visit  https://github.com/golang/go/wiki/GOPATH .

You can see the value of all go related variable using the command go env.

code@code:~/Go/GoLangSeries/Package/Example2$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/code/Go/GoLangSeries/Package/Example2"
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

Now lets try to add emoticon to out code using a package called emoji.  The package can be found here https://gopkg.in/kyokomi/emoji.v1 . This package provides the following emojis https://www.webpagefx.com/tools/emoji-cheat-sheet/.

mkdir Example1 
cd Example1
export GOPATH=GOPATH:$PWD
go get gopkg.in/kyokomi/emoji.v1

Tree view

code@code:~/Go/GoLangSeries/Package/Example1$ tree
.
├── hello.go
├── pkg
│   └── linux_amd64
│          └── gopkg.in
│                └── kyokomi
│                       └── emoji.v1.a
└── src
     └── gopkg.in
            └── kyokomi
                    └── emoji.v1
                          ├── emoji_codemap.go
                          ├── emoji.go
                          ├── emoji_test.go
                          ├── example
                          │    └── hello.go
                          ├── LICENSE
                          ├── README.md
                          └── screen
                                └── image.png

10 directories, 9 files
code@code:~/Go/GoLangSeries/Package/Example1$

If you notice the tree view of the folder the pkg folder contains the emoji library


package main

import (
 "fmt"
"gopkg.in/kyokomi/emoji.v1"
)

func main() {
 fmt.Println("Hello World Emoji!")

emoji.Println(":beer: Beer!!!")

pizzaMessage := emoji.Sprint("I like a :pizza: and :sushi:!!")
 fmt.Println(pizzaMessage)
}

Capture1

Now lets try to create our own package called myutils and install it and then use it. Lets create a package called myutil.go

 
mkdir Example2
cd Example2
export GOPATH=GOPATH:$PWD 
mkdir -p src/myutil
vi src/myutil/myutil.go

 

 
package myutil
import "fmt"
func TestMe() {
fmt.Println("testMe")
}
func CallMe() {
fmt.Println("callMe")
}

Not an important point that the functions should start with a Capital letter, explanation for that can be found here.

 
cd src/myutil/
go install .

 

code@code:~/Go/GoLangSeries/Package/Example2$ tree
.
├── main.go
├── pkg
│    └── linux_amd64
│            └── myutil.a
└── src
      └── myutil
            └── myutil.go

4 directories, 3 files

package main
import "myutil"

func main() {
 myutil.CallMe()
}

myutil.CallMe() actually calls the CallMe function from myutil package.

 

Leave a Reply

Your email address will not be published. Required fields are marked *