DEV Community

Cover image for Understanding Methods in Go
Ganesh Kumar
Ganesh Kumar

Posted on

Understanding Methods 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.

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())
}
Enter fullscreen mode Exit fullscreen mode

Output:

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

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)
}
Enter fullscreen mode Exit fullscreen mode

Output:

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

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…




Top comments (0)