Understanding Methods in Go

posted Originally published at dev.to 6 min read

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.

In my previous post, I explained how closure works.

In this article we will understand how to method works.

Method

Method is a function with a receiver.

This works like a class method in other languages.

Here is the example of method.

let's define a struct value and add method to it.


package main

import "fmt"


type value struct {
    X, Y int
}



func (v value) add() int {

return v.X + v.Y
}

func main() {
    val := value{1,2}

    fmt.Println(val.add())
    val2 := &value{1,2}
    fmt.Println(val2.add())
}

Output:

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

This is a simple example of method.

Pointer receiver

Pointer receiver is used to modify the value of the struct.


package main

import "fmt"


type value struct {
    X, Y int
}



func (v *value) add() int {

v.X += 1
v.Y += 1
return v.X + v.Y
}

func main() {
    val := value{1,2}
    fmt.Println(val.add())
    println(val.X,val.Y)
}

Output:

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

From this we can see that pointer receiver is used to modify the value of the struct.

Conclusion

In this article we understood how method works and how pointer receiver is used to modify the value of 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…




More Posts

Understanding Closures in Go

Ganesh Kumar - Mar 23

Understanding Anonymous Functions in Go

Ganesh Kumar - Mar 23

Introduction to Go generics?

Ganesh Kumar - Mar 30

Understanding Interface in Go

Ganesh Kumar - Mar 31

How I Built a React Portfolio in 7 Days That Landed ₹1.2L in Freelance Work

Dharanidharan - Feb 9
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

4 comments
4 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!