Monday 6 May 2019

Go language : struct literal notation


You can create an object to a structure using literal notation like below.

emp1 := Employee{"Krishna", 1}

HelloWorld.go
package main

import "fmt"

func main() {
 emp1 := Employee{"Krishna", 1}

 fmt.Println("name : ", emp1.name)
 fmt.Println("id : ", emp1.id)
}

type Employee struct{
 name string
 id int
}

Output
name :  Krishna
id :  1

You can list subset of the fields using name of that field.
emp1 := Employee{id: 123}


App.java
package main

import "fmt"

func main() {
 emp1 := Employee{id: 123}

 fmt.Println("name : ", emp1.name)
 fmt.Println("id : ", emp1.id)
}

type Employee struct {
 name string
 id   int
}

Output
name : 

id :  123


Previous                                                 Next                                                 Home

No comments:

Post a Comment