//
package main
import "fmt"
//定義了一 個(gè)空的結(jié)構(gòu)體
type st01 struct {
}
//定義一個(gè)非空的結(jié)構(gòu)體
type person struct {
Name string
Age int
}
func main() {
personInfo := person{}
personInfo.Age = 19
personInfo.Name = "xiaoqiang"
fmt.Println(personInfo)
//------------------------------
fmt.Println("=============================")
//直接初始化 屬于 字面值 初始化
personInfo2 := person{Name :"spark01", Age: 19}
fmt.Println(personInfo2)
}
?
創(chuàng)新互聯(lián)公司專注于青龍企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),成都做商城網(wǎng)站。青龍網(wǎng)站建設(shè)公司,為青龍等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
//結(jié)構(gòu)體,按值 傳遞
package main
import "fmt"
//定義一個(gè)類型
type sparkInfo struct {
SparkClusterName string
SparkNodeNum int
}
func main() {
newSparkInfo := sparkInfo{
SparkNodeNum: 8,
SparkClusterName: "spark-test-001"}
fmt.Println("main:\t", newSparkInfo)
updateSparkInfo(newSparkInfo)
fmt.Println("main:\t", newSparkInfo)
}
//同樣,這里是值傳遞,內(nèi)部的修改,并不會影響到 舊值的
func updateSparkInfo(sparkInfo sparkInfo) {
sparkInfo.SparkNodeNum = 9
fmt.Println("updateSparkInfo:\t", sparkInfo)
}
?
//結(jié)構(gòu)體,按地址傳遞
package main
import "fmt"
type k8sInfo struct {
K8sClusterName string //k8s集群的名稱
K8sClusterNumm int //k8s集群的節(jié)點(diǎn)個(gè)數(shù)
}
func main() {
k8sInfo := k8sInfo{
K8sClusterNumm: 1,
K8sClusterName: "k8s-test-001"}
fmt.Println(k8sInfo)
updateK8sClusterInfo(&k8sInfo)
fmt.Println(k8sInfo)
}
//傳遞的是 地址,按地址傳遞
//修改了,舊的值
func updateK8sClusterInfo(info *k8sInfo) {
info.K8sClusterNumm = 110
fmt.Println("updateK8s:\t", info)
}
?
//結(jié)構(gòu)體,按地址傳遞
package main
import "fmt"
type dockerInfo struct {
DockerClusterName string
DockerClusterNum int
}
func main() {
//一般更習(xí)慣這種寫法,
//在初始化的時(shí)候,就直接獲取地址
//這樣以后在調(diào)用的時(shí)候,就不需要添加&號了
dInfo := &dockerInfo{
DockerClusterNum:19,
DockerClusterName:"docker-yizhuang"}
fmt.Println("init docker:\t", dInfo)
updateDockerInfo(dInfo)
fmt.Println("after docker:\t", *dInfo)
}
func updateDockerInfo (info *dockerInfo) {
info.DockerClusterNum = 80
fmt.Println("udpateDocker:\t", info)
}
?
//匿名結(jié)構(gòu)
package main
import "fmt"
func main() {
//創(chuàng)建一個(gè)匿名結(jié)構(gòu),
//并且進(jìn)行了初始化
//而且,直接獲取地址&
ftp := &struct {
FtpName string
FtpNum int
}{
FtpName:"ftp-beijing",
FtpNum:8}
fmt.Println(ftp)
fmt.Println(*ftp)
fmt.Println("FtpName:\t", ftp.FtpName)
fmt.Println("FtpNum:\t", ftp.FtpNum)
}
?
//將匿名結(jié)構(gòu),嵌套進(jìn)別的結(jié)構(gòu)體里
package main
import "fmt"
type hadoop struct {
HadoopClusterName string
HadoopClusterNum int
//創(chuàng)建一個(gè)匿名結(jié)構(gòu)
HadoopOtherInfo struct {
//同樣,當(dāng)多個(gè)變量都一樣的時(shí)候,也可用省略
//這是Go語言的優(yōu)點(diǎn)
HadoopVersion, HadoopUrl string
}
}
func main() {
hdfs := &hadoop{
HadoopClusterName: "Hadoop-test-001",
HadoopClusterNum: 9}
//只能通過這種方式,進(jìn)行初始化
hdfs.HadoopOtherInfo.HadoopUrl = "http://192.168.1.110:50070"
hdfs.HadoopOtherInfo.HadoopVersion = "v2.7.0"
fmt.Println(hdfs)
fmt.Println(*hdfs)
fmt.Println("HadoopClusterName:\t", hdfs.HadoopClusterName)
fmt.Println("HadoopClusterNum:\t", hdfs.HadoopClusterNum)
fmt.Println("HadoopClusterVersion:\t", hdfs.HadoopOtherInfo.HadoopVersion)
fmt.Println("HadoopClusterUrl:\t", hdfs.HadoopOtherInfo.HadoopUrl)
}
?
//匿名字段 測試
package main
import "fmt"
type students struct {
//這些就是匿名字段,沒有定義名字
string
int
}
func main() {
boy := &students{
//初始化的時(shí)候,必須按照順序來進(jìn)行的
"xiaoqiang", 19}
fmt.Println(boy)
}
?
// 相同結(jié)構(gòu)體 之間 操作,如賦值,比較
package main
import "fmt"
type teacherA struct {
string
int
}
type teacherB struct {
string
int
}
func main() {
boyTeacherA := teacherA{"xiaoli",22}
//boyTeacherB := teacherB{"xiaoli",22}
//説明:編譯報(bào)錯(cuò)了,teacherA, teacherB 類型不相同,不能進(jìn)行比較的
//fmt.Println(boyTeacherA == boyTeacherB)
boyTeacherB := teacherA{"xiaoli", 23}
fmt.Println(boyTeacherB == boyTeacherA)
}
?
//結(jié)構(gòu)體,實(shí)現(xiàn) 類似于 繼承的效果
package main
import "fmt"
type anminal struct {
//設(shè)置一些共有的屬性
Name, address string
}
type cat struct {
//anminal Go 語言,默認(rèn),anminal是類型,同時(shí)也是屬性名稱
anminal
Sex int // 貓的特有屬性,性別是啥
}
type dog struct {
anminal
Hobby string //狗的特有屬性,愛好
}
func main() {
//第一種初始化方式
xiaoCat := cat{Sex:0, anminal : anminal{Name:"xiaohong", address:"beijing"}}
xiaoDog := dog{Hobby:"play", anminal:anminal{Name:"xiaohuang", address:"shanghai"}}
//第二種初始化方式
xiaoCat.anminal.Name = "xiaoxiaoxiaohong" //這種方式,是為了防止名字相同時(shí),沖突
xiaoCat.Name = "xiaoxiaohong"
fmt.Println("cat:\t", xiaoCat)
fmt.Println("dog:\t",xiaoDog)
}
package main
import "fmt"
type annimal2 struct {
name string
// 這種形式,也是可以的
//有點(diǎn)類似于接口中定義方法
show func()
}
func main() {
dog := annimal2{name:"xiaohuang",
show: func() {
fmt.Println("this is xiaohuang")
},
}
dog.show()
cat:= annimal2{name:"xiaohong",
show: func() {
fmt.Println("this is xiaohong")
},
}
cat.show()
}
運(yùn)行結(jié)果:
this is xiaohuang
this is xiaohong
Process finished with exit code 0
分享標(biāo)題:Go語言之struct結(jié)構(gòu)體
網(wǎng)站URL:http://vcdvsql.cn/article2/pehooc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、關(guān)鍵詞優(yōu)化、域名注冊、Google、品牌網(wǎng)站設(shè)計(jì)、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)