-
-
Save MuthukumarHelios/1e09bbb9cf58ea7df717abd4bc310792 to your computer and use it in GitHub Desktop.
Go lang Structs and interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What is Structs | |
Structs are the Collection of Field with data types declared by user: | |
Uses : | |
Rather by maintaining a seperate Data type we can use a struct | |
Example1 --Struct vs Variable | |
package main() | |
import "fmt" | |
//Logger | |
var log = fmt.Println | |
var logT = fmt.Printf | |
// Struct Declarion | |
type abcd struct{ | |
a string | |
b int | |
c bool | |
d float64 | |
} | |
func main(){ | |
//variable declaration | |
var a string = "hello" | |
var b int = 12 | |
var c bool = true | |
var d float64 = 12.34 | |
log("a:",a,"b:",b,"c:",c,"d:",d) | |
// collection of varible i.e Struct | |
abcd_value := abcd{"hello", 12, false, 12.22} | |
logT("struct variables %+v\n",abcd_value) | |
type man struct { | |
age int | |
sex string | |
name string | |
} | |
man_value := man{ | |
12 , | |
"M" , | |
"Your Name"} | |
// log a struc With KEy values | |
logT("struct variables %+v\n",man_value) | |
man_value_with_key := man{ | |
age: 12 , | |
sex: "M" , | |
name: "Your Name"} | |
log(man_value_with_key) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment