Golang Tutorial #3 - Packages 05-02-2016, 03:39 PM
#1
Packages and Libraries in Go look scary at first - when I first saw examples, I was kind of freaking out at how complex they looked, but I was looking at the full, built package that was installed via the "go get" command. Anyway, on with the tut.
Installing/Importing/Running Packages and Libraries
Go doesn't have an official site for package management (like PyPI, RubyGems, or npm), which has both drawbacks and advantages. Expanding on this, there's no metrics for package downloads, but there's also no repository list.
Installing Go packages is easy. After you find a repo for a Go package or library you want to install, simply run the following.
This obviously works for any git repo, but I'd recommend sticking with GitHub, BitBucket, and Launchpad.
If the package has an executable, it will be generated in $GOPATH/bin/, where $GOPATH is the environment variable that the user has to set to install packages. Given this, to run executable, add $GOPATH/bin/ to your PATH variable.
If the package is a library, you would import it as below:
Updating packages is also easy. Just use the command below.
Making packages
If you look over any single-file, executable Go code, you'll see the "package main" declaration on the first line. "main" denotes that the file is, well, the main project file, so Go makes it runnable.
For any other packages, you need to create a subdirectory (following convention, with the name of the package) and replace "main" with the package name for any files inside.
If there are multiple files within a package, they automatically share functions and global variables, so if you needed a lookup table, like this, you don't need to import the file like you would in Python or NodeJS.
Exporting Functions
Function Exports are short and sweet, like most things in Go. Below is a quick comparison of public/protected/private variables in Java vs. Go.
It's a subtle difference, but any variable or function with an uppercase first letter is accessible to any other package.
As a sidenote, package names are always lower case.
For a full example of Go's package structure, check out my repo.
This was a bit of a quick one, and it took a while, since I'm doing all my culminating projects and studying for exams. Not sure what I'm going to do for the next tutorial, but I'll try to get it out faster.
Installing/Importing/Running Packages and Libraries
Go doesn't have an official site for package management (like PyPI, RubyGems, or npm), which has both drawbacks and advantages. Expanding on this, there's no metrics for package downloads, but there's also no repository list.
Installing Go packages is easy. After you find a repo for a Go package or library you want to install, simply run the following.
Code:
go get github.com/user/repoThis obviously works for any git repo, but I'd recommend sticking with GitHub, BitBucket, and Launchpad.
If the package has an executable, it will be generated in $GOPATH/bin/, where $GOPATH is the environment variable that the user has to set to install packages. Given this, to run executable, add $GOPATH/bin/ to your PATH variable.
If the package is a library, you would import it as below:
Code:
// regular sub-package import
import "math/big"
// 3rd party import
import "source/user/repo"
// example
import "github.com/Inori-Y/library"Updating packages is also easy. Just use the command below.
Code:
go get -u source/user/repoMaking packages
If you look over any single-file, executable Go code, you'll see the "package main" declaration on the first line. "main" denotes that the file is, well, the main project file, so Go makes it runnable.
For any other packages, you need to create a subdirectory (following convention, with the name of the package) and replace "main" with the package name for any files inside.
If there are multiple files within a package, they automatically share functions and global variables, so if you needed a lookup table, like this, you don't need to import the file like you would in Python or NodeJS.
Exporting Functions
Function Exports are short and sweet, like most things in Go. Below is a quick comparison of public/protected/private variables in Java vs. Go.
Spoiler: Java
Code:
public class Example{
// public variable, accessible by any class
public int pubInt=10;
// protected variable, accessible to current package
protected int protInt=10;
int defaultInt=10;
// private variable, accessible by this class
private int privInt=10;
}Spoiler: Go
Code:
package example
// equivalent to protected and default
var protInt int=10
// equivalent to public
var PubInt int=10It's a subtle difference, but any variable or function with an uppercase first letter is accessible to any other package.
As a sidenote, package names are always lower case.
For a full example of Go's package structure, check out my repo.
This was a bit of a quick one, and it took a while, since I'm doing all my culminating projects and studying for exams. Not sure what I'm going to do for the next tutorial, but I'll try to get it out faster.
It's often the outcasts, the iconoclasts ... those who have the least to lose because they
don't have much in the first place, who feel the new currents and ride them the farthest.
don't have much in the first place, who feel the new currents and ride them the farthest.















![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)