DEV Community

Cover image for How to create a struct type in Go or Golang?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to create a struct type in Go or Golang?

#go

Originally posted here!

To create a struct type in Go or Golang, you can use the keyword type followed by the name of the struct, then the keyword struct and finally using the {} symbol (opening and closing curly brackets). Inside the brackets, we need to add the fields followed by the type that it needs to hold.

TL;DR

package main

import "fmt"

// Admin struct
type Admin struct {
    name string
    id   int
}

func main() {
    // create a new `Admin`
    // using the `Admin` struct
    // and assign it to the `john` variable
    john := Admin{
        name: "John Doe",
        id:   1,
    }

    // log the contents of the `john` variable to console
    fmt.Printf("%+v", john) // {name:John Doe id:1}
}
Enter fullscreen mode Exit fullscreen mode

The struct types are used to organize relational pieces of data in a single entity. If you are coming from a JavaScript ecosystem, structs are kind of the same as the objects.

For example, let's say we need to create a struct type called Admin with fields of name and id having the types of string and int.

To do that, we have to first use the type keyword followed by the name of the struct Admin then the keyword struct ending with a {} symbol (opening and closing brackets) like this,

package main

// Admin struct
type Admin struct {
    name string
    id   int
}

func main() {
    // cool code here
}
Enter fullscreen mode Exit fullscreen mode

Even though you can choose to declare struct types inside the main() function, my recommendation would be to declare it outside the function as it will be cleaner.

Now let's create a new Admin from the Admin struct type and add some values to its fields.

To do that we can simply write the type Admin followed by the {} symbol (opening and closing curly brackets). Inside the brackets, we can write the fields and their corresponding value separated by the : symbol (semi-colon).

It can be done like this,

package main

// Admin struct
type Admin struct {
    name string
    id   int
}

func main() {
    // create a new `Admin`
    // using the `Admin` struct
    Admin{
        name: "John Doe",
        id:   1,
    }
}
Enter fullscreen mode Exit fullscreen mode

Let's also assign the newly created Admin to a variable called john and then log the contents of it to the console like this,

package main

import "fmt"

// Admin struct
type Admin struct {
    name string
    id   int
}

func main() {
    // create a new `Admin`
    // using the `Admin` struct
    // and assign it to the `john` variable
    john := Admin{
        name: "John Doe",
        id:   1,
    }

    // log the contents of the `john` variable to console
    fmt.Printf("%+v", john) // {name:John Doe id:1}
}
Enter fullscreen mode Exit fullscreen mode

As you can see that the values of John Doe and 1 are displayed in the console which proves that the new Admin is created.

See the above code live in The Go Playground.

That's all πŸ˜ƒ!

Feel free to share if you found this useful πŸ˜ƒ.


Top comments (0)