Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Windows Tipps & SecurityWindows-Update beschädigt wichtige Datenrettungsfunktion(22.09.2026 um 09:04 Uhr)
Sichere ProgrammierungBuilding an Accessible Ecommerce Product Page with WCAG 2.2(22.09.2026 um 03:39 Uhr)
Sichere ProgrammierungGet Your Website Protected in 10 Minutes with SafeLine WAF(22.09.2026 um 08:42 Uhr)
Sichere ProgrammierungIntroduction to SPRINGBOOT(22.09.2026 um 08:42 Uhr)
Windows Tipps & SecurityWindows-Update beschädigt wichtige Datenrettungsfunktion(22.09.2026 um 09:04 Uhr)
Sichere ProgrammierungBuilding an Accessible Ecommerce Product Page with WCAG 2.2(22.09.2026 um 03:39 Uhr)
Sichere ProgrammierungGet Your Website Protected in 10 Minutes with SafeLine WAF(22.09.2026 um 08:42 Uhr)
Sichere ProgrammierungIntroduction to SPRINGBOOT(22.09.2026 um 08:42 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Value Receivers vs Pointer Receivers in Go (A Practical Explanation)

One of the first real design questions you hit in Go is: Should this method use a value receiver or a pointer receiver? You’ll see both forms everywhere: func (u User) Greet() {} and func (u *User) UpdateName() {} They l…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

One of the first real design questions you hit in Go is:



Should this method use a value receiver or a pointer receiver?



You’ll see both forms everywhere:




func (u User) Greet() {}






and




func (u *User) UpdateName() {}






They look similar, but the choice matters for correctness, performance, and how your types behave in real backend code.



Let’s break it down properly.









What is a Receiver?



In Go, a method is just a function attached to a type.




type User struct {
Name string
}






Now we add behavior:




func (u User) Greet() {
fmt.Println("Hello,", u.Name)
}






The u here is called the receiver. It’s the value the method operates on.



You call it like this:




user := User{Name: "Shivam"}
user.Greet()






So far so good.









Value Receiver: The Method Gets a Copy



When you write:




func (u User) ChangeName() {
u.Name = "New Name"
}






The method receives a copy of the struct.



That means it can read the data, but if it modifies the receiver, it’s only modifying the copy.



Example:




type User struct {
Name string
}

func (u User) ChangeName() {
u.Name = "New Name"
}

func main() {
user := User{Name: "Shivam"}
user.ChangeName()

fmt.Println(user.Name)
}






Output:




Shivam






Nothing changed, because the method was working on a copy.



This is the most important thing to understand:



Value receiver means “work on a copy”.









Pointer Receiver: The Method Works on the Original



Now compare that with:




func (u *User) ChangeName() {
u.Name = "New Name"
}






Here the receiver is a pointer, so the method has access to the original struct in memory.



Example:




type User struct {
Name string
}

func (u *User) ChangeName() {
u.Name = "New Name"
}

func main() {
user := User{Name: "Shivam"}
user.ChangeName()

fmt.Println(user.Name)
}






Output:




New Name






This time the change sticks, because pointer receivers modify the real object.



Pointer receiver means “work on the original”.









When Should You Use Pointer Receivers?



In production Go code, pointer receivers are the default for most structs.



You want pointer receivers when the method needs to modify state. Any method that updates fields should almost always be a pointer receiver.



They’re also important for performance. If your struct is large, using value receivers means copying it every time you call a method. Pointer receivers avoid that copy.



Another big reason is interfaces. In Go, methods with pointer receivers belong only to the pointer type, not the value type. That affects whether your type satisfies an interface, which matters a lot in backend systems.









When Are Value Receivers Fine?



Value receivers are good when the struct is small and the method doesn’t need to modify anything.



A common example is something like a Point type or a read-only helper method.




func (p Point) Distance() float64






If the method is purely about reading data, value receivers are clean and safe.









One Practical Rule That Works Most of the Time



If your type represents something real in a backend system (service, handler, config, DB model, controller), use pointer receivers.



If your type is small and behaves like a simple value, value receivers are fine.



When in doubt, pointer receivers are usually the right choice.









Simply



Value receiver: method works on a copy.

Pointer receiver: method works on the original.



That’s the whole difference, and once you internalize it, Go method design becomes straightforward.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Value Receivers vs Pointer Receivers in Go (A Practical Explanation)

Thematisch verwandte Begriffe: Value, Receivers, Pointer, Practical · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-55210 | Joplin is an open source note-taking and to-do application that organise…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick