您好,登錄后才能下訂單哦!
//
package main
import "fmt"
//定義了一 個空的結構體
type st01 struct {
}
//定義一個非空的結構體
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)
}
?
//結構體,按值 傳遞
package main
import "fmt"
//定義一個類型
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)
}
//同樣,這里是值傳遞,內部的修改,并不會影響到 舊值的
func updateSparkInfo(sparkInfo sparkInfo) {
sparkInfo.SparkNodeNum = 9
fmt.Println("updateSparkInfo:\t", sparkInfo)
}
?
//結構體,按地址傳遞
package main
import "fmt"
type k8sInfo struct {
K8sClusterName string //k8s集群的名稱
K8sClusterNumm int //k8s集群的節點個數
}
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)
}
?
//結構體,按地址傳遞
package main
import "fmt"
type dockerInfo struct {
DockerClusterName string
DockerClusterNum int
}
func main() {
//一般更習慣這種寫法,
//在初始化的時候,就直接獲取地址
//這樣以后在調用的時候,就不需要添加&號了
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)
}
?
//匿名結構
package main
import "fmt"
func main() {
//創建一個匿名結構,
//并且進行了初始化
//而且,直接獲取地址&
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)
}
?
//將匿名結構,嵌套進別的結構體里
package main
import "fmt"
type hadoop struct {
HadoopClusterName string
HadoopClusterNum int
//創建一個匿名結構
HadoopOtherInfo struct {
//同樣,當多個變量都一樣的時候,也可用省略
//這是Go語言的優點
HadoopVersion, HadoopUrl string
}
}
func main() {
hdfs := &hadoop{
HadoopClusterName: "Hadoop-test-001",
HadoopClusterNum: 9}
//只能通過這種方式,進行初始化
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{
//初始化的時候,必須按照順序來進行的
"xiaoqiang", 19}
fmt.Println(boy)
}
?
// 相同結構體 之間 操作,如賦值,比較
package main
import "fmt"
type teacherA struct {
string
int
}
type teacherB struct {
string
int
}
func main() {
boyTeacherA := teacherA{"xiaoli",22}
//boyTeacherB := teacherB{"xiaoli",22}
//説明:編譯報錯了,teacherA, teacherB 類型不相同,不能進行比較的
//fmt.Println(boyTeacherA == boyTeacherB)
boyTeacherB := teacherA{"xiaoli", 23}
fmt.Println(boyTeacherB == boyTeacherA)
}
?
//結構體,實現 類似于 繼承的效果
package main
import "fmt"
type anminal struct {
//設置一些共有的屬性
Name, address string
}
type cat struct {
//anminal Go 語言,默認,anminal是類型,同時也是屬性名稱
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" //這種方式,是為了防止名字相同時,沖突
xiaoCat.Name = "xiaoxiaohong"
fmt.Println("cat:\t", xiaoCat)
fmt.Println("dog:\t",xiaoDog)
}
package main
import "fmt"
type annimal2 struct {
name string
// 這種形式,也是可以的
//有點類似于接口中定義方法
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()
}
運行結果:
this is xiaohuang
this is xiaohong
Process finished with exit code 0
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。