Go Receiver Function
By ski11up.com
Go lang is an open-source programming language like C, having pointers and not strongly typed language. And most importantly it is not an Object Oriented Programming(debatable) language though it has likes of defining packages
and interfaces
like Java
.
Go
has a concept of receiver function that is unique in any programming language so far. The receiver function is a function that is tied to a specific type of data.
Let’s explore the Go
Receiver
function and do a comparison with the equivalent Java
code to get more understanding.
App
We will define a Contact Details
having FirstName
, LastName
and Email
, initialize it and print the same on the console.
Go
Go
code for printing the contact details using the receiver function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main
import "fmt"
type contact struct { (1)
FirstName string
LastName string
Email string
}
func main() {
john := contact{ (2)
FirstName: "John",
LastName: "Doe",
Email: "jd@gmail.com",
}
john.print() (4)
}
// receiver function
func (c contact) print() { (3)
fmt.Printf("Contact Details : %s", c)
}
1 | defining the struct type for storing contact |
2 | initializing the contact |
3 | this is our receiver function, it only takes struct of type contact, so the function receives the type just like taking an argument but it should be of type contact and later on the method body it is printing the value of the contact which we have initialized earlier |
4 | this is how we used receiver function, receiver function is being called on a struct , imagine toString() function in Java |
Java
Java
code for printing the contact details.
public class PrintContact {
static Map<String, String> contact = new HashMap<>(); (1)
public static void print() { (2)
contact.forEach( (k, v) -> {
System.out.println(k + v);
});
}
public static void main(String[] args) {
contact.put("FistName", "John"); (3)
contact.put("LastName", "Doe");
contact.put("Email", "jd@gmail.com");
PrintContact pc = new PrintContact(); (4)
pc.print(); (5)
}
}
1 | defining a data structure for storing contact details |
2 | class method to pring the class variable |
3 | intialializing the data structure with contact details |
4 | we have to intialize the PrintContact class to utilize print() method |
5 | now we can call print() on the object of PrintContact |
Execute Go App
Navigate to the project folder and execute the below command.
$ go run print_contact.go
Execute Java App
Navigate to the project folder and execute the below command.
$ javac PrintContact.java
$ java PrintContact
Notes
You can see from the above Go
and Java
code for doing the same thing, it is notable that in Go
we don’t require an object to use the print()
function. Moreover, the print()
method is available inside the class
, in Go
the contact
struct
has no knowledge about the print()
method. Note that in Go
print()
is being called on the data structure whereas in Java
it is called from the object
for the known reasons.
The receiver function is an interface and the function to be called will be determined dynamically. We can also say the way programmer wants to write its code in a way to be more readable like instead of print my contact (which will be print(c contact)
), say it like contact to print (contact.print()
).