GOLANG

Anatomy of methods in Go

Go does not support the Object-Oriented paradigm but structure resembles the class architecture. To add methods to a structure, we need to use functions with a receiver.

Uday Hiwarale
Oct 20, 2018 · 12 min read
(source: pexels.com)

What is a method?

https://play.golang.org/p/ANE_IN9cmk4
func (r Type) functionName(...Type) Type {
...
}
https://play.golang.org/p/68JWEHO9Yep

Methods with the same name

https://play.golang.org/p/3f_MScaNXUw

Pointer receivers

https://play.golang.org/p/cV-W27SGcnl
func (r *Type) functionName(...Type) Type {
...
}
https://play.golang.org/p/PfSQ_GP-GPN

Calling methods with pointer receiver on values

https://play.golang.org/p/D2zm7lpSme4

Methods on nested struct

https://play.golang.org/p/8_QhLqqb9Ot

Methods on nested struct

https://play.golang.org/p/DBikGzozbAy

Anonymously nested struct

https://play.golang.org/p/d7D8FYYmiof

Promoted methods

https://play.golang.org/p/x2L-rR-TKNF

Methods can accept both pointer and value

https://play.golang.org/p/tiQQbimhQ8_O

Methods on non-struct type

https://play.golang.org/p/FVvIh-YQBLH
program.go:8: cannot define new methods on non-local type string
program.go:14: str.toUpperCase undefined (type string has no field or method toUpperCase)
https://play.golang.org/p/N-lLB3xPSDM

RunGo

A go-to guide for learning Go programming language

Uday Hiwarale

Written by

Software Engineer at kausa.ai / thatisuday.com ☯ github.com/thatisuday ☯ thatisuday@gmail.com

RunGo

RunGo

A place to find introductory Go programming language tutorials and learning resources. In this publication, we will learn Go in an incremental manner, starting from beginner lessons with mini examples to more advanced lessons.

Uday Hiwarale

Written by

Software Engineer at kausa.ai / thatisuday.com ☯ github.com/thatisuday ☯ thatisuday@gmail.com

RunGo

RunGo

A place to find introductory Go programming language tutorials and learning resources. In this publication, we will learn Go in an incremental manner, starting from beginner lessons with mini examples to more advanced lessons.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store