visit
So, today’s bug and a lesson to be learned is about non/pointer receiver function, it happens when I’m doing custom marshal JSON for my struct. I’m stuck for a few hours when to fix this.
So the scenario is, I have a struct let’s say a ClassRoom struct, and the ClassRoom has many students. Every student ranked ascending by their mark or something whatever it is :D, this is just an example to describe how is our real case struct. It’s similar, but different in name.
type ClassRoom struct {
Name string `json:"name"`
Students map[int]Student `json:"students"`
}
type Students struct {
Name string `json:"name"`
Age int `json:"age"`
}
{
"name": "D3TI2-Classmate",
"students": {
"1": {
"name": "Iman Tumorang",
"age": 17
},
"2": {
"name": "Christin Tumorang",
"age": 18
},
"3": {
"name": "Idola Manurung",
"age": 18
}
}
}
{
"name": "D3TI2-Classmate",
"students": [
{
"name": "Iman Tumorang",
"age": 17
},
{
"name": "Christin Tumorang",
"age": 18
},
{
"name": "Idola Manurung",
"age": 18
}
]
}
func (s ClassRoom) MarshalJSON() ([]byte, error) {
arrStudent := []Student{}
arrKey := []int{}
for k, _ := range s.Students {
arrKey = append(arrKey, k)
}
sort.Ints(arrKey)
for _, pos := range arrKey {
arrStudent = append(arrStudent, s.Students[pos])
}
return json.Marshal(struct {
Name string `json:"name"`
Students []Student `json:"students"`
}{
Name: s.Name,
Students: arrStudent,
})
}
Issue
Before the code fixed, the code is more like this below. If we see here, it’s like nothing wrong here. But the result is wrong.package main
import (
"encoding/json"
"fmt"
"log"
"sort"
)
func main() {
students := map[int]Student{
1: Student{
Name: "Iman Tumorang",
Age: 17,
},
2: Student{
Name: "Christin Tumorang",
Age: 18,
},
3: Student{
Name: "Idola Manurung",
Age: 18,
},
}
sample := ClassRoom{
Name: "D3TI2-Classmate",
Students: students,
}
jbyt, err := json.Marshal(sample)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(jbyt))
}
type ClassRoom struct {
Name string `json:"name"`
Students map[int]Student `json:"students"`
}
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
}
func (s *ClassRoom) MarshalJSON() ([]byte, error) {
arrStudent := []Student{}
arrKey := []int{}
for k, _ := range s.Students {
arrKey = append(arrKey, k)
}
sort.Ints(arrKey)
for _, pos := range arrKey {
arrStudent = append(arrStudent, s.Students[pos])
}
return json.Marshal(struct {
Name string `json:"name"`
Students []Student `json:"students"`
}{
Students: arrStudent,
Name: s.Name,
})
}
sample := &ClassRoom{ // See the ampersand symbol "&"
Name: "D3TI2-Classmate",
Students: students,
}
_,_= json.Marshal(sample)
So to summarize:
If I made the custom MarshalJSON with a pointer receiver
func (s *ClassRoom)MarshalJSON() ([]byte, error){
// Other codes
}
Then when I want to marshal the Object, I must pass a pointer object to
json.Marhsal
sample := &ClassRoom{ // See the ampersand symbol "&"
Name: "D3TI2-Classmate",
Students: students,
}
_,_= json.Marshal(sample)
And it also vise versa for non-pointer (value) receiver.
func (s ClassRoom)MarshalJSON() ([]byte, error){
// Other codes
}
If I made the custom MarshalJSON with a non-pointer (value) receiver
func (s ClassRoom)MarshalJSON() ([]byte, error){
// Other codes
}
Then when I want to marshal the Object, I must pass a non-pointer (value) object to
json.Marhsal
sample := ClassRoom{ // Without the ampersand symbol "&"
Name: "D3TI2-Classmate",
Students: students,
}
_,_= json.Marshal(sample)
Well, this is a very silly issue for me, because it’s really made me furious for a few hours. And it’s made me more cautious when deciding using a pointer or non pointer receiver in function.