• No results found

Install Third-Party Packages

In document Web Development with Go.pdf (Page 35-39)

The Go developer community is very enthusiastic about providing many useful third-party packages through code-sharing web sites such as github.com and code.google.com. You can import and reuse these third-party packages by using the Go tools. The go get command fetches packages from remote repositories.

The following go get command fetches the third-part package negroni and installs it into the GOPATH location:

ChapTer 2 ■ Go FundamenTals

The go get command fetches the package and dependent packages recursively from the repository location. Once the package is fetched into the GOPATH, you can import and reuse these packages from all the programs located in the GOPATH location. In many other developer ecosystems, you have to import these packages at a project level; you have to install packages for each individual project separately. When you import a package in Go, you actually import from a common location: the GOPATH pkg directory, so you can appreciate the simplicity and pragmatism in many of the Go features, including the package ecosystem.

Writing Packages

Let’s write a sample package to reuse with other programs. Listing 2-6 is a simple package that swaps characters’ case from upper- to lowercase or lower- to uppercase.

Listing 2-6. Library Package package strcon

import ( "bytes" "unicode" )

// Swap characters case from upper to lower or lower to upper. func SwapCase(str string) string {

buf := &bytes.Buffer{} for _, r := range str { if unicode.IsUpper(r) { buf.WriteRune(unicode.ToLower(r)) } else { buf.WriteRune(unicode.ToUpper(r)) } } return buf.String() }

The package is named strcon. The idiomatic way to provide a package name is to give short and simple lowercase names without underscores or mixed capital letters. The package names of the standard library are a great reference for naming packages.

Let’s build and install the package strcon to be used with other programs. The package provides a method named SwapCase that swaps the character case of a string from upper- to lowercase or lower- to uppercase. Reuse the packages bytes and unicode from the standard library to swap character case. Because the name of the SwapCase method starts with an uppercase letter, it will be exported to other programs when

ChapTer 2 ■ Go FundamenTals

this package is referenced package. The SwapCase method iterates through a string and changed the case of each character: for _, r := range str { if unicode.IsUpper(r) { buf.WriteRune(unicode.ToLower(r)) } else { buf.WriteRune(unicode.ToUpper(r)) } }

The keyword range allows you to iterate through arrays and collections. By iterating through a string value, you can extract each character as a value and swap the character case. On the left side of the range block, you can provide two variables for getting the key and value of each item in the collection. In this code block, the value for getting the character value is used, but you don’t use the key in the program. In this context, you can use a blank identifier (_) to avoid compiler errors. It is common practice to use a blank identifier with range whenever you want to ignore a key or value variable declaration from the left side.

With the following command at the location of the package directory, build the package and install it on the pkg subdirectory of GOPATH:

go install

Let’s write a sample program to reuse the code of the strconv package (see Listing 2-7). Listing 2-7. Reusing the strconv Package in main.go

package main import ( "fmt" "strcon" ) func main() { s := strconv.SwapCase("Gopher") fmt.Println("Converted string is :", s) }

We import the package strcon to reuse the code for swapping character case in a string. Let’s run the program by typing the following command in the terminal from the package directory:

go run main.go

You should see the following result when running the program:

gOPHER

Because the program in Listing 2-7 is written in package main, the Go build command generates an executable binary into the package directory. The Go install command builds the package and installs the resulting binary into the GOPATH bin subdirectory.

ChapTer 2 ■ Go FundamenTals

Go Tool

The Go tool is a very important component of the Go ecosystem. In the previous sections, you used the Go tool to build and run Go programs. In the terminal, type the go command without any parameters to get documentation on the commands provided by the Go tool.

Here is the documentation on Go commands: Go is a tool for managing Go source code. Usage:

go command [arguments] The commands are:

build compile packages and dependencies clean remove object files

doc show documentation for package or symbol env print Go environment information

fix run go tool fix on packages fmt run gofmt on package sources

generate generate Go files by processing source

get download and install packages and dependencies install compile and install packages and dependencies

list list packages

run compile and run Go program

test test packages

tool run specified go tool version print Go version

vet run go tool vet on packages

Use "go help [command]" for more information about a command. Additional help topics:

c calling between Go and C

buildmode description of build modes filetype file types

gopath GOPATH environment variable environment environment variables importpath import path syntax

packages description of package lists testflag description of testing flags testfunc description of testing functions

Use "go help [topic]" for more information about that topic. For documentation on any specific command type:

ChapTer 2 ■ Go FundamenTals

Here is the command for getting documentation for the install command:

go help install

Here is the documentation for the install command: usage: go install [build flags] [packages]

Install compiles and installs the packages named by the import paths, along with their dependencies.

For more about the build flags, see 'go help build'. For more about specifying packages, see 'go help packages'. See also: go build, go get, go clean.

In document Web Development with Go.pdf (Page 35-39)