How to create a struct type in Go or Golang?
Tags: go
Published: 2022-08-28 14:47:52

---
  title: How to create a struct type in Go or Golang?
  published: true
  tags: Go
  date: Sun Aug 28 2022 20:17:52 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/create-struct-type-go-golang
  cover_image: https://melvingeorge.me/_next/static/images/main-51aca4882e738f1c701ab6822ad60d7c.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/create-struct-type-go-golang)
  
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

```go
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}
}
```

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,

```go
package main

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

func main() {
    // cool code here
}
```

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,

```go
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,
    }
}
```

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,

```go
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}
}
```

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](https://go.dev/play/p/_9Hq5UYQ9SS).

That's all 😃!

### Feel free to share if you found this useful 😃.

---