xxxxxxxxxx
A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct.
xxxxxxxxxx
package main
import (
"encoding/json"
"fmt"
"log"
)
// sigle struct
type Contact struct {
id int
name string
phone int
}
// nested struct
type ShowRoom struct {
address string
location string
postalcode int
}
// sub nested struct
type Car struct {
id int
name string
price float32
showroom ShowRoom
}
// same struct type defination
type Person struct {
Firstname, Lastname string
}
// json struct
type Book struct {
Name string `json:"name"`
Price float32 `json:"price"`
Author string `json:"author"`
}
// struct as parameter
func GetContact(contact Contact) {
fmt.Println("get struct with as parameter", contact)
}
// read struct with method
func (supercar Car) GetSuperCar() {
fmt.Println("read struct with method", supercar)
}
func main() {
var ContactPerson Contact
var SuperCar Car
var Biodata Person
abstractStruct := struct {
Name string
Age int
}{
Name: "john doe",
Age: 23,
}
var MyBook Book
ContactPerson.id = 1
ContactPerson.name = "john doe"
ContactPerson.phone = 87887242895
NewContactPerson := Contact{2, "jane doe", 8777888555666}
GetContactPointer := &ContactPerson
SuperCar.id = 1
SuperCar.name = "lambogini"
SuperCar.price = 1.500000000
SuperCar.showroom.address = "jl.nusantara kap 50 - 51"
SuperCar.showroom.location = "depok"
SuperCar.showroom.postalcode = 163353
Biodata.Firstname = "aldi"
Biodata.Lastname = "khan"
MyBook.Name = "hary potter"
MyBook.Price = 50.000
MyBook.Author = "jk.rolling"
json, err := json.Marshal(MyBook)
fmt.Println("read struct step one", ContactPerson)
fmt.Println("read struct step two", NewContactPerson)
fmt.Printf("read struct by key %v\n", ContactPerson.name)
GetContact(ContactPerson)
fmt.Println("read struct with pointer", *GetContactPointer)
fmt.Println("read struct by key with pointer", (*GetContactPointer).phone)
fmt.Println("read nested struct", SuperCar)
fmt.Println("read nested struct with key", SuperCar.showroom.location)
SuperCar.GetSuperCar()
fmt.Println("read struct with same type defination", Biodata)
fmt.Println("read abstract struct", abstractStruct)
if err != nil {
log.Fatal(err)
} else {
fmt.Println("read struct with json style", string(json))
}
}
xxxxxxxxxx
// A struct is a type. It's also a collection of fields
// Declaration
type Vertex struct {
X, Y int
}
// Creating
var v = Vertex{1, 2}
var v = Vertex{X: 1, Y: 2} // Creates a struct by defining values with keys
var v = []Vertex{{1,2},{5,2},{5,5}} // Initialize a slice of structs
// Accessing members
v.X = 4
// You can declare methods on structs. The struct you want to declare the
// method on (the receiving type) comes between the the func keyword and
// the method name. The struct is copied on each method call(!)
func (v Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
// Call method
v.Abs()
// For mutating methods, you need to use a pointer (see below) to the Struct
// as the type. With this, the struct value is not copied for the method call.
func (v *Vertex) add(n float64) {
v.X += n
v.Y += n
}
xxxxxxxxxx
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Address struct {
Street string `json:"street"`
Suite string `json:"suite"`
Zipcode string `json:"zipcode"`
}
type Users struct {
Name string `json:"name"`
Age uint `json:"age"`
}
func ObjectStruct() {
// users 1 example
var users1 struct {
Name string `json:"name"`
Age uint `json:"age"`
}
users1.Name = "john doe"
users1.Age = 23
// users 2 example
users2 := Users{
Name: "john doe",
Age: 23,
}
// users 3 example
var users3 Users
users3.Name = "john doe"
users3.Age = 23
// users 4 example
var users4 Users = Users{
Name: "john doe",
Age: 23,
}
// users 5 example
var users5 = struct {
Name string `json:"name"`
Age uint `json:"age"`
}{
Name: "john doe",
Age: 23,
}
// users 6 example
users6 := struct {
Name string `json:"name"`
Age uint `json:"age"`
}{
Name: "john doe",
Age: 23,
}
fmt.Printf("Object struct 1 %#v \n", users1)
fmt.Printf("Object struct 2 %#v \n", users2)
fmt.Printf("Object struct 3 %#v \n", users3)
fmt.Printf("Object struct 4 %#v \n", users4)
fmt.Printf("Object struct 5 %#v \n", users5)
fmt.Printf("Object struct 6 %#v \n", users6)
}
func ArrayObjectStruct() {
// users 1 example
users1 := []Users{
Users{
Name: "john doe",
Age: 23,
},
}
// users 2 example
var users2 []Users
users2 = []Users{
Users{
Name: "john doe",
Age: 23,
},
}
// users 3 example
var users3 []Users = []Users{
Users{
Name: "john doe",
Age: 23,
},
}
// users 4 example
users4 := make([]Users, 1)
users4 = []Users{
Users{
Name: "john doe",
Age: 23,
},
}
// users 5 example
var users3 []Users = []Users{
Users{
Name: "john doe",
Age: 23,
},
}
fmt.Printf("Array object struct 1 %#v \n", users1)
fmt.Printf("Array object struct 2 %#v \n", users2)
fmt.Printf("Array object struct 3 %#v \n", users3)
fmt.Printf("Array object struct 4 %#v \n", users4)
}
func main() {
ObjectStruct()
fmt.Printf("\n")
ArrayObjectStruct()
}