Golang: Struct

By Xah Lee. Date: . Last updated: .

Struct is user-defined type. It defines a collection of “fields”, each field is a name and a value of a specific type.

Struct is similar to golang map, or python dictionary, ruby hashtable, JavaScript object, except that number of keys and key names are fixed, predefined, as a user-created type.

Once you defined a struct type (with a given name), you can create struct of that name.

Note, struct is very important in golang. because methods, and interface, are based on it.

Define a New Struct Type

Here's how to define a struct.

type Name struct { name1 type1 name2 type2 … }

here's how to create a struct you just defined:

struct_name{val1, val2 …}


struct_name{name1:val1, name2:val2 …}

package main

import "fmt"

// define a struct named PP
type PP struct {
    x int
    y int

func main() {
    // create a struct PP
    var x PP = PP{1, 2}

    // print struct with value only
    fmt.Printf("%v\n", x) // {1, 2}

    // print struct with field name and value
    fmt.Printf("%+v\n", x) // {x:1 y:2}

Create Struct: Struct Literals

A struct literal lets you create a value of a given struct type.

For example, if you have this struct type

type PP struct{ x, y int }

you can create a value by

var p1 = PP{x:3, y:4}


var p1 = PP{}

and use PP.x=3 etc to fill the field values later.

package main

import "fmt"

// define a struct type
type PP struct{ x, y int }

func main() {

    // create a struct of PP
    var p1 = PP{x: 3, y: 4}

    fmt.Printf("%#v\n", p1) // main.PP{x:3, y:4}


If a key is omitted, it defaults to its zero value.

[see Zero Value]

package main

import "fmt"

type PP struct{ x, y int }

func main() {
    var p1 = PP{y: 4}
    fmt.Printf("%#v\n", p1) // main.PP{x:0, y:4}

Alternatively, you can omit keys and only use values.

var p1 = PP{3, 4}

If you omit keys, the order must be the same as the struct type is defined, and none of element must contain a key.

package main

import "fmt"

type PP struct{ x, y int }

func main() {
    var tt = PP{3, 4}
    fmt.Printf("%#v\n", tt) // main.PP{x:3, y:4}

Get/Set Field Value

Struct fields are accessed using a dot.

package main

import "fmt"

type PP struct {
    x int
    y int

func main() {

    v := PP{3, 4}

    // get a field value
    fmt.Println(v.x) // 3

    // set a field value
    v.x = 4
    fmt.Println(v.x) // 4

Print Struct

[see Golang: Print]

Pointer to structs

[see Pointer]


The Go Programming Language Specification - The Go Programming Language#Struct_types

If you have a question, put $5 at patreon and message me.


  1. Compile, Run
  2. Source Encoding
  3. Package, Import
  4. Comment
  5. Print
  6. String
  7. Rune
  8. String as Chars
  9. Variable
  10. Zero Value
  11. Constant
  12. If Then Else
  13. Switch/Case
  14. Loop
  15. Basic Types
  16. Array
  17. Slice
  18. Map
  19. Struct
  20. Function
  21. regexp
  22. Read File
  23. Write to File
  24. Walk Dir
  25. Check File Exist
  26. System Call
  27. Get Script Path
  28. Pointer
  29. Defer
  30. Random Number


  1. match any regexp
  2. Validate Links
  3. Generate Sitemap


  1. Go Spec