go structs

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

4.25
8

                                    type Person struct {
  name string
  surname string
}

func (p Person) GetName() string {
  return p.name
}

func (p Person) GetSurname() string {
  return p.surname
}

4.25 (8 Votes)
0
0
0

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

0
0
Are there any code examples left?
New code examples in category Go
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
go create C struct struct in struct golang golang type struct go struct of struct create struct go\ go struct static method golang set of structs how to use struct golang struct struct go struct call struct struct go how to declare a struct go what all can go in golang struct golang spew struct struct go lang define structs in go golang struct and interface go calling method on struct Go call method on struct golang struct for golang struct string golang string struct golang structs output struct on golang making a struct go how to make a new struct golang go struct with func golang method in struct how to create a struct go golang method inside struct go struct ` go make struct are go struct golang struct literal golang ways to define a struct golang struct of struct golang create a struct go := struct { golang is struct how to create struct in golang implementing struct method golang what is a struct in golang how to create a golang struct how to define [] in struct golang golang * struct golang & and * struct struct in go EXAMPLE golang struct in go EXAMPLE struct in go "syntax" how to create struct in go struct with function golang make struct golang create a struct in golang golang struct gorm golang == in struct golang struct or struct golang struct properties golang struct String() making structs in go golang struct new why we need struct in golang make a new struct golang string() method struct go go struct use method of an element go struct use a element method example struct golang Golang struct example '[]struct' in go []struct in go what is Structs in go golang struct of structs struct in golang examples golang literal struct declare struct golang declare a struct go golang struct from struct struct of structs golang define a struct in golang struct function go when do we use struct in golang golang struct ini go struct function golang make struct golang struct attributes basic golang structs struct and method in golang go struct with function struct method name golang create a struct golang struct functions go golang new struct golang function on struct declare a struct in golang golang struct method how to define a struct golang golang structure struct golang go struct type golang struct to interface golang struct interface golang array of structs golang create struct struct object example golang struct example golang golang struct in []struct go lang struct create struct in golang declaring struct golang struct go create struct golang golang struct in struct function to struct golang struct in go go define struct in struct define struct golang go structs golang struct with functions struct.go structs go go functions on struct go struct structs in go struct in golang define function in struct golang golang struct methods define struct in golang using struct in go sruct in go go example struct golang -loop struct create struct in go initialize struct golang golang struct type golang function structure type struct golang golang struct golang methods go struct example struct method golang golang object methods changine instance values golang object methods golang struct how to define a member function golang struct how to define a member variable function go strcut method golang attach function to struct golang struct function user method in go struct methods golang get all methods of an object golang struct func golang how to implement struct functions in golang changing struct inside method golang struct methods go golang function in struct golang struct make function golang functions part of struct golang method example struct functions golang struct function golang function as a struct member go go lang struct type function go struct methods
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source