golang array of structs code example

Example 1: how to make struct type as array in go

package main

import "fmt"

type rectangle struct {
	length  float64
	breadth float64
	color   string
}

func main() {
	var rect1 = rectangle{10, 20, "Green"}
	rect2 := rectangle{length: 20, breadth: 10, color: "Red"}

	if rect1 == rect2 {
		fmt.Println("True")
	} else {
		fmt.Println("False")
	}

	rect3 := new(rectangle)
	var rect4 = &rectangle{}

	if rect3 == rect4 {
		fmt.Println("True")
	} else {
		fmt.Println("False")
	}
}

Example 2: golang struct

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))
	}
}

Example 3: how to make struct type as array in go

package main

import "fmt"

type Salary struct {
	Basic, HRA, TA float64
}

type Employee struct {
	FirstName, LastName, Email string
	Age                        int
	MonthlySalary              []Salary
}

func (e Employee) EmpInfo() string {
	fmt.Println(e.FirstName, e.LastName)
	fmt.Println(e.Age)
	fmt.Println(e.Email)
	for _, info := range e.MonthlySalary {
		fmt.Println("===================")
		fmt.Println(info.Basic)
		fmt.Println(info.HRA)
		fmt.Println(info.TA)
	}
	return "----------------------"
}

func main() {

	e := Employee{
		FirstName: "Mark",
		LastName:  "Jones",
		Email:     "[email protected]",
		Age:       25,
		MonthlySalary: []Salary{
			Salary{
				Basic: 15000.00,
				HRA:   5000.00,
				TA:    2000.00,
			},
			Salary{
				Basic: 16000.00,
				HRA:   5000.00,
				TA:    2100.00,
			},
			Salary{
				Basic: 17000.00,
				HRA:   5000.00,
				TA:    2200.00,
			},
		},
	}

	fmt.Println(e.EmpInfo())
}

Example 4: how to make struct type as array in go

package main

import "fmt"

type rectangle struct {
	length  int
	breadth int
	color   string
}

func main() {
	var rect1 = rectangle{10, 20, "Green"}
	fmt.Println(rect1)

	var rect2 = rectangle{length: 10, color: "Green"} // breadth value skipped
	fmt.Println(rect2)

	rect3 := rectangle{10, 20, "Green"}
	fmt.Println(rect3)

	rect4 := rectangle{length: 10, breadth: 20, color: "Green"}
	fmt.Println(rect4)

	rect5 := rectangle{breadth: 20, color: "Green"} // length value skipped
	fmt.Println(rect5)
}

Tags:

Go Example