Menu Close

Go – Print structure variables in console in go golang

In this tutorial, we are going to learn about how to print structure variables in console in go golang.

To print structure fields with name in console, we use "%+v". 

Example:

fmt.Printf("%+v", structure_variable_name) 
package main

import (
        "fmt"
)

type stud struct {
    name string
    roll int
    marks int
}

func main() {
     s := stud{"Bob", 10, 100}

     fmt.Printf("%+v", s)
}

Output:

{name:Bob roll:10 marks:100}

Structure with json format:

package main

import (
        "fmt"
)

type stud struct {
    name string `json: "stud_name"`
    roll int `json: "stud_roll"`
    marks int `json: "marks"`
}

func main() {
     s := stud{"john", 10, 100}

     fmt.Printf("%+v", s)
}

Output:

{name:john roll:10 marks:100} 

To learn more about golang, You can refer given below link:

https://www.techieindoor.com/go-lang-tutorial/
https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println
Posted in golang

Leave a Reply

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