惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - 陈峰

yamux多路复用的使用例子 golang channel golang context - 陈峰 - 博客园 发布spring cloud + vue项目 以太坊上发行ERC20代币 [转]大白话讲解Promise(一) 比特币测试网络搭建 非对称加密, 助记词, PIN, WIF 搭建EOS未完 - 陈峰 - 博客园 解决 Unknown action buyram in contract eosio 错误 [转]EOS智能合约 & 私链激活 & 基本操作 [转]https://www.jianshu.com/p/06443248f4d8 web3js 进行转账 [转]How to Send Ethereum with Web3.js and Node 【转】干货 | 【虚拟货币钱包】从 BIP32、BIP39、BIP44 到 Ethereum HD Wallet 钱包,卡 [转]vux使用教程 [转]CentOS7利用systemctl添加自定义系统服务 [转]重定向输出
golang struct
陈峰 · 2019-03-13 · via 博客园 - 陈峰

ex1

/*
https://golangbot.com/structs/
struct 结构
结构就是一组字段。
*/

package main

import "fmt"

// 声明一个结构
type Employee struct {
	firstName string
	lastName  string
	age       int
}
//// 同类型简化声明
//type Employee struct {
//	firstName, lastName string
//	age, salary         int
//}

func main() {

	// 匿名结构, 不给结构名字
	var employee struct {
		firstName, lastName string
		age int
	}
	employee.age = 33
	fmt.Println(employee)

	// 结构初始化为零値
	fmt.Println("Employee:", Employee{})
}

ex2

// 创建命名结构
package main

import (
	"fmt"
)

type Employee struct {
	firstName, lastName string
	age, salary         int
}

func main() {

	//使用字段名字创建结构
	emp1 := Employee{
		firstName: "Sam",
		age:       25,
		salary:    500,
		lastName:  "Anderson",
	}

	//创建结构不使用字段名字
	emp2 := Employee{"Thomas", "Paul", 29, 800}

	fmt.Println("Employee 1", emp1)
	fmt.Println("Employee 2", emp2)
}

ex3

// 创建匿名结构
package main

import (
	"fmt"
)

func main() {
	emp3 := struct {               // 这里定义
		firstName, lastName string
		age, salary         int
	}{                            // 这里初始化
		firstName: "Andreah",
		lastName:  "Nikola",
		age:       31,
		salary:    5000,
	}

	fmt.Println("Employee 3", emp3)
}

ex4

// 结构字段访问
package main

import (
	"fmt"
)

type Employee struct {
	firstName, lastName string
	age, salary         int
}

func main() {
	emp6 := Employee{"Sam", "Anderson", 55, 6000}
	fmt.Println("First Name:", emp6.firstName)
	fmt.Println("Last Name:", emp6.lastName)
	fmt.Println("Age:", emp6.age)
	fmt.Printf("Salary: $%d", emp6.salary)
	fmt.Println()
	var emp7 Employee
	emp7.firstName = "Jack"
	emp7.lastName = "Adams"
	fmt.Println("Employee 7:", emp7)
}

ex5

// 结构指针
package main

import (
	"fmt"
)

type Employee struct {
	firstName, lastName string
	age, salary         int
}

func main() {
	// 这里emp8就是一个结构指针
	emp8 := &Employee{"Sam", "Anderson", 55, 6000}
	fmt.Println("First Name:", (*emp8).firstName)  // 通过指针访问结构字段
	fmt.Println("Age:", (*emp8).age)

	// golang提供另一种可选方法来访问结构字段
	fmt.Println("First Name:", emp8.firstName)
	fmt.Println("Age:", emp8.age)
}

ex6

// 结构匿名字段
package main

import (
	"fmt"
)

// 定义的结构, 包含有匿名字段
type Person struct {
	string
	int
}

func main() {
	// 带匿名字段的结构的实例化
	p1 := Person{"Naveen", 50}
	fmt.Println(p1)
	// 尽管匿名字段没有名字, 它的缺省名字是类型名字
	p1.string = "naveen_new"
	p1.int = 100
	fmt.Println(p1)
}

ex7

// 结构嵌套
package main

import (
	"fmt"
)

type Address struct {
	city, state string
}
type Person struct {
	name string
	age int
	address Address
}

func main() {
	var p Person
	p.name = "Naveen"
	p.age = 50
	p.address = Address {
		city: "Chicago",
		state: "Illinois",
	}
	fmt.Println("Name:", p.name)
	fmt.Println("Age:",p.age)
	fmt.Println("City:",p.address.city)
	fmt.Println("State:",p.address.state)
}

ex8

// 结构提升字段
package main

import (
	"fmt"
)

type Address struct {
	city, state string
}
type Person struct {
	name string
	age  int
	Address  // 当结构中, 存在一个匿名的结构型字段。  我们把这个字段叫提升字段
}

func main() {
	var p Person
	p.name = "Naveen"
	p.age = 50
	p.Address = Address{
		city:  "Chicago",
		state: "Illinois",
	}
	fmt.Println("Name:", p.name)
	fmt.Println("Age:", p.age)
	fmt.Println("City:", p.city) //这个字段,直接提升到上一级,可以直接访问
	fmt.Println("State:", p.state) //这个字段,直接提升到上一级,可以直接访问
	fmt.Println("City:", p.Address.city)
	fmt.Println("State:", p.Address.state)
}

ex9

// 结构导出<即外部可访问性>
package main

import (
	"fmt"
)

// 当前结构首字母大写, 则外部包可以访问
type Address struct {
	// 字段同理
	City, state string
}
type Person struct {
	name string
	age  int
	Address  // 当结构中, 存在一个匿名的结构型字段。  我们把这个字段叫提升字段
}

func main() {
	var p Person
	p.name = "Naveen"
	p.age = 50
	p.Address = Address{
		City:  "Chicago",
		state: "Illinois",
	}
	fmt.Println(p)

}

ex10

// 结构相等
// 结构是值类型,可以直接比较(仅当他们字段可以比较时)
package main

import (
	"fmt"
)

type name struct {
	firstName string
	lastName string
}

type image struct {
	data map[int]int  // 这个字段是不可比较的
}

func main() {
	name1 := name{"Steve", "Jobs"}
	name2 := name{"Steve", "Jobs"}
	if name1 == name2 {
		fmt.Println("name1 and name2 are equal")
	} else {
		fmt.Println("name1 and name2 are not equal")
	}

	name3 := name{firstName:"Steve", lastName:"Jobs"}
	name4 := name{}
	name4.firstName = "Steve"
	if name3 == name4 {
		fmt.Println("name3 and name4 are equal")
	} else {
		fmt.Println("name3 and name4 are not equal")
	}


	// 不可比较情况
	//image1 := image{data: map[int]int{
	//	0: 155,
	//}}
	//image2 := image{data: map[int]int{
	//	0: 155,
	//}}
	//// 这里直接报错
	//if image1 == image2 {
	//	fmt.Println("image1 and image2 are equal")
	//}
}