Most main editors have some support for Go. Check the full list of IDEs and Plugins for Go on the official Go Wiki.
Though many editors support Go, they do not offer the same level of features and integrations.
These are things you might look for when considering an editor, from the most basic to the more advanced use cases. Keep in mind people tend to have different opinions here, so this is just a list to let you know some of what is possible, value the ones you care about most:
[Read More]
Create the first test for your program
The convention in Go is to put unit tests alongside the code that they test rather than in a different directory. This has the advantage of giving a very visible way of seeing what has tests and what doesn’t. All test files are named *_test.go; anything named that way will not be added to the final binary, but will be taken into account by go test.
Create a file main_test.go and add the following code:
[Read More]
Create your first Go program
Lets write our first hello world program.
(1) Create a src directory in $GOPATH, which is /root/gowork in my system:
# mkdir src # tree . └── src 1 directory, 0 files (2) Since Go organizes source code using “package” concept , and every “package” should occupy a distinct directory, I create a greet directory in src:
Create a directory in your workspace:
mkdir -p $GOPATH/src/github.com/<your_user>/hello cd $GOPATH/src/github.com/<your_user>/hello touch main.go Open your favorite editor, and add the following to main.
[Read More]
Installing some common tools
Use go get to download packages and their dependencies, then install them:
go get github.com/golang/lint/golint \ golang.org/x/tools/cmd/goimports Golint looks for common code style mistakes. Golint makes suggestions for many of the mechanically checkable items listed in Effective Go and the CodeReviewComments wiki page.
goimports formats your code correctly (it’s a drop-in replacement for gofmt) and also removes any unused imports. It will also try to resolve imports that haven’t been added to the imports definition.
[Read More]
Some common Go commands that you should know
go build(see below): builds / compiles your code into a binary or librarygo install(see below): the same asgo build, plus installs binaries into $GOPATH/bin and libraries to $GOPATH/pkggo get: downloads source code and dependencies, then perform ago installgo test(see below): compile and run tests in the given package(s)go vet: check code for common coding errors
Create Go workspace
Once the Go build environment is ready, the next step is to create workspace for development:
(1) Set up a new empty directory - This is where your projects can reside:
mkdir $HOME/gowork (2) Use a new environment variable $GOPATH to point it:
# cat /etc/profile ...... GOPATH=$HOME/gowork export GOPATH ...... If you are using bash or zsh, put the following in the respective file:
export GOPATH=$HOME/gowork The workspace should contain 3 subdirectories:
[Read More]
Create greet package
mkdir src/greet Then create a new Go source code file (greet.go) in src/greet:
# cat src/greet/greet.go package greet import "fmt" func Greet() { fmt.Println("नमस्ते किंशुक!") } You can consider this greet directory provides a greet package which can be used by other programs.
(3) Create another package hello which utilizes the greet package:
# mkdir src/hello # cat src/hello/hello.go package main import "greet" func main() { greet.Greet() } You can see in hello.
[Read More]
Debugging
No one can write bug-free code, so debugging is a requisite skill for every software engineer. Here are some tips about debugging Go programs:
(1) Print
Yes! Printing logs seems the easiest method, but it is indeed the most effective approach in most cases. Go has provided a big family of printing functions in fmt package, and using them neatly is an expertise you should grasp.
(2) Debugger
In some scenarios, maybe you need the specialized debugger tools to help you spot the root cause.
[Read More]
How to build Go development environment
Build Go development environment is always easy. Take Linux OS as an example (Because I work as a root user, so if you login as a non-root user, maybe you need sudo to execute some commands), what you should do is just download the binary package which matches your system from here, and uncompress it:
# wget https://storage.googleapis.com/golang/go1.6.2.linux-amd64.tar.gz # tar -C /usr/local/ -xzf go1.6.2.linux-amd64.tar.gz Now, there is an extra go directory under /usr/local.
[Read More]