go语言练习:结构体

package main

import "fmt"

type book struct {
	title  string
	author string
	page   int
}

func main() {
	var book1 book
	book1.author = "Adong"
	book1.page = 100
	book1.title = "Story of Adong"
	fmt.Println(book1.author, book1.page, book1.title)
}


//输出结果:
Adong 100 Story of Adong

  

package main

import "fmt"

type book struct {
	title  string
	author string
	page   int
}

func main() {
	var book1 book
	book1.author = "Adong"
	book1.page = 100
	book1.title = "Story of Adong"
	print_book(book1)
}

func print_book(b book) {            //这里函数接收到的参数是结构体
	fmt.Println(b.author, b.page, b.title)
}

结构体指针:

package main

import "fmt"

type book struct {
	title  string
	author string
	page   int
}

func main() {
	var book1 book
	book1.author = "Adong"
	book1.page = 100
	book1.title = "Story of Adong"
	print_book(book1)
	var ptr *book = &book1
	fmt.Println("book=", ptr.title)
}

func print_book(b book) { //这里函数接收到的参数是结构体
	fmt.Println(b.author, b.page, b.title)
}

  

猜你喜欢

转载自www.cnblogs.com/chenadong/p/9051433.html