Golang Organizing Data With Structs

 

 

 

 

Declaring Structs

package main

import "fmt"

type person struct {
	firstName string
	lastName  string
}

func main() {
	alex := person{firstName: "Alex", lastName: "Anderson"}
	fmt.Println(alex)
}

Updating Structs Values

package main

import "fmt"

type person struct {
	firstName string
	lastName  string
}

func main() {
	var alex person

	alex.firstName = "Alex"
	alex.lastName = "Anderson"

	fmt.Println(alex)
	fmt.Printf("%+v", alex)
}

 

Embedding Structs

package main

import "fmt"

type contactInfo struct {
	email   string
	zipCode int
}

type person struct {
	firstName string
	lastName  string
	contact   contactInfo
}

func main() {
	maxwell := person{
		firstName: "Maxwell",
		lastName:  "Pan",
		contact: contactInfo{
			email:   "maxwell@gmail.com",
			zipCode: 96500,
		},
	}

	fmt.Printf("%+v", maxwell)
}

 

Structs with Receiver Functions

package main

import "fmt"

type contactInfo struct {
	email   string
	zipCode int
}

type person struct {
	firstName string
	lastName  string
	contactInfo
}

func main() {
	maxwell := person{
		firstName: "Maxwell",
		lastName:  "Pan",
		contactInfo: contactInfo{
			email:   "maxwell@gmail.com",
			zipCode: 96500,
		},
	}

	maxwell.updateName("Max")
	maxwell.print()
}

func (p person) updateName(newFirstName string) {
	p.firstName = newFirstName
}

func (p person) print() {
	fmt.Printf("%+v", p)
}

 

Pass By Value

Structs with Pointers

 

package main

import "fmt"

type contactInfo struct {
	email   string
	zipCode int
}

type person struct {
	firstName string
	lastName  string
	contactInfo
}

func main() {
	maxwell := person{
		firstName: "Maxwell",
		lastName:  "Pan",
		contactInfo: contactInfo{
			email:   "maxwell@gmail.com",
			zipCode: 96500,
		},
	}

	maxwellPointer := &maxwell
	maxwellPointer.updateName("Max")
	maxwell.print()
}

func (pointerToPerson *person) updateName(newFirstName string) {
	(*pointerToPerson).firstName = newFirstName
}

func (p person) print() {
	fmt.Printf("%+v", p)
}

Pointer Operations

 

 

 

 

Pointer Shortcut

package main

import "fmt"

type contactInfo struct {
	email   string
	zipCode int
}

type person struct {
	firstName string
	lastName  string
	contactInfo
}

func main() {
	maxwell := person{
		firstName: "Maxwell",
		lastName:  "Pan",
		contactInfo: contactInfo{
			email:   "maxwell@gmail.com",
			zipCode: 96500,
		},
	}

	maxwell.updateName("Max")
	maxwell.print()
}

func (pointerToPerson *person) updateName(newFirstName string) {
	(*pointerToPerson).firstName = newFirstName
}

func (p person) print() {
	fmt.Printf("%+v", p)
}

 

Gotchas With Pointers

package main

import "fmt"

func main() {
	mySlice := []string{"Hi", "There", "How", "Are", "You"}
	updateSlice(mySlice)
	fmt.Println(mySlice)
}

func updateSlice(s []string) {
	s[0] = "Bye"
}

 

 Test Your Knowledges: Pointers

 

 

 

 

 

 

 

 

Reference vs Value Type

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值