Custom Search

Friday, August 19, 2016

Go Golang How to pretty print struct variable

package main

/* */
import (

/*A struct is a type which contains named fields.
Define new type named 'Person'*/
type Person struct {
name string
age  int

func main() {

/*initialize a struc.
 Create an instance of type 'Person'*/
p := Person{"Sam", 20}

fmt.Println("====", p)

/* %v  print the value in a default format*/
fmt.Printf("=====%v \n", p)

/*With field name*/
fmt.Printf("======%+v \n", p)

/*With type and field name*/
/* %#v a Go-syntax representation of the value*/
fmt.Printf("=======%#v \n", p)

$go run print_struct.go
==== {Sam 20}
====={Sam 20} 
======{name:Sam age:20} 
=======main.Person{name:"Sam", age:20} 

1 comment: