Golang: Debuging Go code


In this post I will show you some easy (tricky) ways to debug go code (forget about gdb, it’s too hard to understand).

Print Struct as string

package main

import (
    "fmt"
)

type Person struct {
    Name, Address       string
    Age, Height, Weight int
}

func main() {
    Yusuf := Person{
        Name:    "Yoesoff",
        Address: "Bandung Barat",
        Age:     17,
        Height:  165,
        Weight:  70,
    }   

    fmt.Println(Yusuf) // Print struct as string
}

Output:

{Yoesoff Bandung Barat 17 165 70}
{Yoesoff Bandung Barat 17 165 70}

Use fmt.Printf to find Variable / Object type in Golang

fmt.Printf("%T\n",v)
package main

import (
    "fmt"
)

type Person struct {
    Name, Address       string
    Age, Height, Weight int
}

func main() {
    Yusuf := Person{
        Name:    "Yoesoff",
        Address: "Bandung Barat",
        Age:     17,
        Height:  165,
        Weight:  70,
    }   

     fmt.Printf("%T\n", A)
     fmt.Printf("%T\n", B)
     fmt.Printf("%T\n", Yusuf)
}

Output:

string
int
main.Person

Use reflect to find Variable / Object type in Golang

package main

import (
	"fmt"
	"reflect"
)

type Person struct {
	Name, Address       string
	Age, Height, Weight int
}

func main() {
	Yusuf := Person{
		Name:    "Yoesoff",
		Address: "Bandung Barat",
		Age:     17,
		Height:  165,
		Weight:  70,
	}

	Yuka := &Person{
		"Zha",
		"Bandung Barat",
		16,
		165,
		60,
	}

	A := 10
	B := 12
	C := A + B
	D := "I AM A BATMAN"

	fmt.Println(reflect.TypeOf(Yusuf))
	fmt.Println(reflect.TypeOf(Yuka))
	fmt.Println(reflect.TypeOf(*Yuka))
	fmt.Println(reflect.TypeOf(C))
	fmt.Println(reflect.TypeOf(D))
}

Output:

main.Person
*main.Person
main.Person
int
string

Using go-spew to debug Go data structures (Object and Variable)

Installation

$ go get -u github.com/davecgh/go-spew/spew

  package main

  import (
      "github.com/davecgh/go-spew/spew"
  )

  type Person struct {
      Name, Address       string
      Age, Height, Weight int
  }

  func main() {
      Yusuf := Person{
          Name:    "Yoesoff",
          Address: "Bandung Barat",
          Age:     17,
          Height:  165,
          Weight:  70,
      }

      A := 10
      B := 12
      C := A + B
      D := "Wakwau Golang si Golang"

      spew.Dump(Yusuf)
      spew.Dump(C)
      spew.Dump(D)
  }

Output:

(main.Person) {
Name: (string) (len=7) “Yoesoff”,
Address: (string) (len=13) “Bandung Barat”,
Age: (int) 17,
Height: (int) 165,
Weight: (int) 70
}
(int) 22

(string) (len=23) “Wakwau Golang si Golang”

Using godebug A cross-platform debugger for Go.

Installation:

$ go get github.com/mailgun/godebug

1. install-godebug

Setup Go executable binary path :

2. Setup Go Binari

3. Go-Debug-Running

Insert a breakpoint anywhere in a source file you want to debug:

_ = "breakpoint"

package main

  import "fmt"

  type Person struct {
      Name, Address       string
      Age, Height, Weight int
  }

  func main() {
      Yusuf := Person{
          Name:    "Yoesoff",
          Address: "Bandung Barat",
          Age:     17,
          Height:  165,
          Weight:  70,
      }

      _ = "breakpoint"

      A := 10
      B := 12
      C := A + B
      D := "Wakwau Golang si Golang"
      _ = "breakpoint"

      fmt.Println(A)
      fmt.Println(B)
      fmt.Println(C)
      fmt.Println(D)
      fmt.Println(Yusuf)
  }

Output:

6.Godebugx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s