Golang : Dealing with struct's private part
One of the most common things in Golang that easily confuse a newbie is how to declare variable/constant/identifier name properly. Many programmers are able to compile their code successfully, but left puzzled by missing/empty data after being processed. The reason is because the variable/identifier name starts with small cap and in essence turning the variable/identifier into a private type. (see https://golang.org/ref/spec#Exported_identifiers )
IF for some reason that you need to use a private variable inside a struct, the way to manipulate the private variable is to associate methods to it.
For the code example below, the email
variable is private type inside the Person struct and the SetEmail() and Email() methods are created specifically to deal with the email
variable.
Here you go :
package main
import "fmt"
import "encoding/json"
type Person struct {
Name string // 1st letter cap is public, while small cap is private
email string
Gender string
Age int
}
func (p *Person) SetEmail(email string) {
p.email = email
}
func (p Person) Email() string {
return p.email
}
var v = []byte(`[
{
"name": "Denis Silva Costa",
"email": "d*@gmail.com",
"gender": "Male",
"age": 27
},
{
"name": "Ariana Cursino",
"email": "a@a.com",
"gender": "Female",
"age": 31
}
]`)
func main() {
var people []Person
err := json.Unmarshal(v, &people)
if err != nil {
}
// email WILL NOT be displayed because it is private
fmt.Println(people)
// set data to private variable via SetEmail method
people[0].SetEmail("d@gmail.com")
people[1].SetEmail("a@a.com")
// Retrieve data from private variables via Email method
fmt.Println(people[0].Email())
fmt.Println(people[1].Email())
}
Output :
[{Denis Silva Costa Male 27} {Ariana Cursino Female 31}]
d@gmail.com
a@a.com
Hope this short tutorial can be useful to new comers to Golang. Happy coding!
Oh, by the way, the original title for this article is "Golang : Dealing with private variable in struct". ;-)
Reference :
See also : Golang : fmt.Println prints out empty data from struct
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.7k Golang : Simple image viewer with Go-GTK
+15.7k Golang : How to tell if a file is compressed either gzip or zip ?
+4.5k Golang : Intercept, inject and replay HTTP traffics from web server
+6.4k Golang : Rename part of filename
+15.3k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+4.4k Python : Delay with time.sleep() function example
+25.5k PHP : Convert(cast) string to bigInt
+8.2k Golang : Extract or copy items from map based on value
+5.3k PHP : Proper way to get UTF-8 character or string length
+12.2k Golang : Read XML elements data with xml.CharData example
+14.9k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+5.4k CodeIgniter : form input set_value cause " to become & quot