Skip to content

Instantly share code, notes, and snippets.

@MuthukumarHelios
Last active January 26, 2018 06:11
Show Gist options
  • Save MuthukumarHelios/1e09bbb9cf58ea7df717abd4bc310792 to your computer and use it in GitHub Desktop.
Save MuthukumarHelios/1e09bbb9cf58ea7df717abd4bc310792 to your computer and use it in GitHub Desktop.
Go lang Structs and interfaces
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