//
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。