Hello Go – Scratching the surface

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


package main
import (

func main() {
  fmt.Printf("hello, world\n")


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


package rect_test

import "testing"


func TestRect(t *testing.T){ // Test prefix is required
  fmt.Println("In test rect")

func ExamplePrefix() {  // Example prefix is required
  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
go test -v rect

Other debugging and errors-

  1. 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.


Yash Sharma is a Big Data & Machine Learning Engineer, A newbie OpenSource contributor, Plays guitar and enjoys teaching as part time hobby.
Talk to Yash about Distributed Systems and Data platform designs.

One thought on “Hello Go – Scratching the surface

Leave a Reply

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