[Golang] Custom Type Declarations and Struct

Technically, Go is not an object-oriented programming language. It doesn’t have classes, objects, and inheritance. However, you can define custom tyes with methods(receiver functions) as alternative in Go. For exmaple:

decks.go

package main

import "fmt"

// Create a new type of 'deck'
// which is a slice of strings
type decks []string

// 'd' is the copy of the variable of type 'decl'
// that has access to this NewStandardDeck() method
func (d decks) newStandardDeck() decks {
	newDecks := []string{}
	suits := [4]string{"Clubs", "Diamonds", "Hearts", "Spades"}
	ranks := [13]string{"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}

	for _, s := range suits {
		for _, r := range ranks {
			newDecks = append(newDecks, s+" "+r)
		}
	}
	return newDecks
}

func (d decks) print() {

	for _, value := range d {
		fmt.Println(value)
	}
}

main.go

package main

func main() {
	deck := decks{"Test"}
	deck = deck.newStandardDeck()
	deck.print()
}

Struct

A struct is a user-defined type which contains properties that are related together.
For example, we can define a struct Card to reprecent a card in a standard deck.

// Create a new type call Card
type card struct {
	suit string
	rank string
}

// Create a new type of 'deck'
// which is a slice of strings
type decks []card

// 'd' is the copy of the variable of type 'decl'
// that has access to this NewStandardDeck() method
func (d decks) newStandardDeck() decks {
	newDecks := []card{}
	suits := [4]string{"Clubs", "Diamonds", "Hearts", "Spades"}
	ranks := [13]string{"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}

	for _, s := range suits {
		for _, r := range ranks {

			// you can initial card via "card{suit: s, rank: r}" as well
			newDecks = append(newDecks, card{s, r})
		}
	}
	return newDecks
}

func (d decks) print() {
	for _, value := range d {
		fmt.Println(value.suit, value.rank)
	}
}

func (d decks) printWithField() {
	for _, value := range d {
		fmt.Printf("%+v\n", value)
	}
}


We can use struct in a struct as well.


type contactInfo struct {
	phone string
	email string
}

type player struct {
	name    string
	coins   int
	contact contactInfo
}

func main() {
	player1 := player{name: "Brandon", coins: 1000}
	player1.contact = contactInfo{"0987123456", "[email protected]"}

	player2 := player1

	fmt.Printf("%+v\n", player1)
}

Structs Are Pass By Value

When you assign one struct variable to another, a new copy of the struct is created and assigned. For example:

player1 := player{name: "Brandon", coins: 1000}
player1.contact = contactInfo{"0987123456", "[email protected]"}

player2 := player1
player2.name = "Kobe"

fmt.Printf("%s\n", player1.name) // output = Brandon
fmt.Printf("%s\n", player2.name) // output = Kobe

Comparation Between Variables of Same Struct

Because Go does not support overloading of methods and operators, you have to implement function for comparing two variables of same struct.


type contactInfo struct {
	phone string
	email string
}

type player struct {
	name    string
	coins   int
	contact contactInfo
}

func (a player) isRicherThan(b player) bool {
	if a.coins > b.coins {
		return true
	}
	return false
}

func main() {

	player1 := player{name: "Brandon", coins: 5000}
	player2 := player{name: "Mark", coins: 1000}

	if player1.isRicherThan(player2) {
		fmt.Printf("%s has more coins.\n", player1.name)
	}

	if !player2.isRicherThan(player1) {
		fmt.Printf("%s has less coins.\n", player2.name)
	}
}


<<:  (Day 20) Object.create 建立多层继承

>>:  [铁人赛 Day05] React 中的 Code splitting(代码分离)方法

Day 28:专案07 - 天气小助理02 | LINE Notify

图片来源:https://3c.ltn.com.tw/news/45392 现在已经是人手一机的时...

Day15 - this&Object Prototypes Ch3 Objects - Iteration 开头

搭配 in 的 for 回圈会搜寻物件中所有 property 为 enumerable 者 而使...

Day 29 / DL x RL / RL 总结与发展

RL 子系列到这边要告一段落了,整个系列文也接近尾声。RL 是个很有趣的主题,有很多内容可以介绍,但...

Day30 :【TypeScript 不用学了】我终於完赛了!

终於完赛了!最近跑去画画,附上一幅《小王子与小狐狸》,小王子里面,最喜欢的就是他们的故事,是说画画...

DAY 27 如何使用PyImgur

获得已上传图片资讯 修改自官网范例 import pyimgur CLIENT_ID = "...