DEV Community

Cover image for How to Work with Structs and Pointers in Go
Ganesh Kumar
Ganesh Kumar

Posted on

How to Work with Structs and Pointers in Go

#go

Hello, I'm Ganesh Kumar. I'm working on git-lrc: a Git hook for Checking AI generated code.
AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.
git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

Here is the corrected version of your blog post with improved grammar and flow, while maintaining your personal style:

In my previous post, we learned about pointers and how it is used.

Let's learn about struct data type.

Struct Data Type

Struct is a user-defined data type in Go that allows you to group fields of different types together.

Structs can be initialized in multiple ways.

  1. Using field names
  2. Using field positions

Here is an example of a struct initialized using field names:

Initialization using named fields

we have use type keyword to define a struct.

package main

import "fmt"

type developer struct {
    Name string
    Age  int
}

func main() {
    person := developer{Name: "John", Age: 30}
    fmt.Println(person)
}
Enter fullscreen mode Exit fullscreen mode

For above example we have defined a struct developer with two fields Name and Age.

For initializing the struct we have used field names.

Where under developer struct we have defined two fields Name and Age with John and 30 values.

Let's see the output.

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
{John 30}

Enter fullscreen mode Exit fullscreen mode

As we can see the output is {John 30}.

This is how fmt.Println works with struct data type.

Accessing Fields

We can access the fields of a struct using the dot operator.

let's print each data one by one.

package main

import "fmt"

type developer struct {
    Name string
    Age  int
}

func main() {
    person := developer{Name: "John", Age: 30}
    fmt.Println(person.Name)
    fmt.Println(person.Age)
}
Enter fullscreen mode Exit fullscreen mode

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
John
30

Enter fullscreen mode Exit fullscreen mode

We could able to access the fields of a struct using the dot operator.

Now let's see how we can initialize the struct using field positions.

Initializing Struct with field positions

package main

import "fmt"

type developer struct {
    Name string
    Age  int
}

func main() {
    person := developer{"John", 30}
    fmt.Println(person)
}
Enter fullscreen mode Exit fullscreen mode

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
{John 30}

Enter fullscreen mode Exit fullscreen mode

We got same output as previous example.

Now let's see how we can use pointer to modify the struct.

Pointer to Struct

Define pointer to struct is same as define pointer to any other data type.

package main

import "fmt"

type developer struct {
    Name string
    Age  int
}

func main() {
   d := developer{Name: "John", Age: 30}
   p := &d
   fmt.Println(d)
   p.Name = "Jane"
   p.Age = 1e9
   fmt.Println(d)
}
Enter fullscreen mode Exit fullscreen mode

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go 
{John 30}
{Jane 1000000000}
Enter fullscreen mode Exit fullscreen mode

As we can see the output is {John 30} and {Jane 1000000000}.

Conclusion

In this we learned about struct data type and used pointer to modify the struct.

git-lrc

πŸ‘‰ Check out: git-lrc
Any feedback or contributors are welcome! It’s online, open-source, and ready for anyone to use.
⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit

git-lrc logo

git-lrc

Free, Unlimited AI Code Reviews That Run on Commit


git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • πŸ€– AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • πŸ” Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • πŸ” Build a habit, ship better code. Regular review β†’ fewer bugs β†’ more robust code β†’ better results in your team.
  • πŸ”— Why git? Git is universal. Every editor, every IDE, every AI…




Top comments (0)