Golang Methods Explained

Glitch
3 min readMar 21, 2023

--

Go, or Golang, is a powerful programming language that has gained significant popularity in recent years. It is a compiled language that was developed by Google in 2009, and it is designed to be efficient, simple, and scalable. One of the main strengths of Go is its support for concurrency, which makes it a popular choice for building web servers and distributed systems.

In this blog, we will explore some of the methods in Go that developers can use to build robust and efficient applications.

Functions
Functions are an essential part of any programming language, and Go is no exception. In Go, functions are defined using the func keyword, followed by the function name and a set of parentheses that can include zero or more parameters. Here is an example of a simple function in Go:
func add(a, b int) int {
return a + b
}
In this example, we define a function called add that takes two integer parameters and returns their sum. Functions in Go can also return multiple values, making it easier to write concise and expressive code. For example:
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, fmt.Errorf("cannot divide by zero")
}
return a / b, nil
}
In this example, we define a function called divide that takes two float64 parameters and returns two values: the quotient of the two numbers and an error, which is returned if the second parameter is zero.

Methods
Methods in Go are similar to functions, but they are associated with a specific type. A method is defined using the func keyword, followed by the method name, the receiver, and a set of parentheses that can include zero or more parameters. Here is an example of a method in Go:
type Person struct {
Name string
Age int
}

func (p Person) Greet() {
fmt.Printf("Hello, my name is %s and I am %d years old\n", p.Name, p.Age)
}
In this example, we define a method called Greet that is associated with the Person struct. The receiver for this method is a Person value, which allows us to access the properties of the struct using the . operator.

Methods can also have pointer receivers, which allows them to modify the properties of the associated struct. Here is an example of a method with a pointer receiver:
func (p *Person) SetAge(age int) {
p.Age = age
}
In this example, we define a method called SetAge that takes an integer parameter and updates the Age property of the associated Person struct.

Interfaces
Interfaces in Go provide a way to define a set of methods that a type must implement to satisfy the interface. This makes it possible to write generic functions and methods that can work with any type that satisfies the interface.

To define an interface in Go, we use the type keyword, followed by the interface name and a set of method signatures. Here is an example of an interface in Go:
type Animal interface {
Speak() string
}
In this example, we define an interface called Animal that requires any implementing type to have a Speak method that returns a string.

Any type that implements all of the methods in an interface is said to satisfy that interface. Here is an example of a type that satisfies the Animal interface:
type Dog struct {
Name string
}

func (d Dog) Speak() string {
return "Woof!"
}

--

--

No responses yet