Export Go path
export GOPATH=/Users/ysharma/work/go/
Directory Structure
.
├── bin
│ ├── main
│ └── test
├── pkg
│ └── darwin_amd64
│ └── rect.a
└── src
├── main
│ └── hello.go
└── rect
├── rect.go
└── rect_test.go
Bin – Where the binary files land
Pkg – Dir for the packages of your code
Src – Your source files
Main – a package for the entry point for code
Rect – Library for our code, has rect.go (source library) and rect_test.go (test case for library rect)
Code walkthrough
src/main/hello.go
package main
import (
"fmt"
"rect"
)
func main() {
fmt.Printf("hello, world\n")
rect.Print()
}
src/rect/rect.go
package rect
import "fmt"
type rect struct {
width int
height int
}
func (r *rect) area() int {
return r.width * r.height
}
func Print() {
r := rect{width: 10, height: 5}
fmt.Println("area:", r.area()) // this adds an extra space
}
src/rect/rect_test.go
package rect_test
import "testing"
import(
"fmt"
"rect"
)
func TestRect(t *testing.T){ // Test prefix is required
fmt.Println("In test rect")
rect.Print()
}
func ExamplePrefix() { // Example prefix is required
fmt.Println("hello")
rect.Print() // Below Output comments are required, else Go compiles test but skips the run
// Output:
// hello
// area: 50
}
Build and run code
go install rect go install main ./bin/main go test -v rect
Other debugging and errors-
- No Testcases : make sure the function have a proper Prefix and definition Example or Test. Also make sure that the Example has // Output comments, else Go skips running them.


Pingback: Create a basic distributed system in Go lang – Part 1 – ConfusedCoders