惜风不起、唯有努力!
golang结构体定义简单使用

golang结构体定义简单使用

package main

import "fmt"

//结构体定义,一般可用于定义一类事物,重复调用,注意外部调用首字母要大写。
type dog struct {
	name string
	age int
	aihao []string  //切片
}

type nums struct {
	maxs int
	floats int
}

func main(){
	var dog dog   //实列化
	dog.name = "小黑"
	dog.age = 2
	dog.aihao = make([]string,0)
	dog.aihao = append(dog.aihao,"吃饭")
	dog.aihao = append(dog.aihao,"睡觉")
	dog.aihao = append(dog.aihao,"看大门")
	num := nums{17, 33}
	fmt.Printf("nums info>>>>[%d],[%d]\n",num.maxs,num.floats)
	fmt.Println("狗info==")
	fmt.Printf("dog name: %s \n",dog.name)
	fmt.Printf("dog age: %d \n", dog.age)
	fmt.Printf("dog aihao: %s,%s,%s",dog.aihao[0],dog.aihao[1],dog.aihao[2])
}

输出结果如下图

发表回复

您的电子邮箱地址不会被公开。