- 定义一个结构体
type Book struct {title stringauth string
}
- 结构体使用
package mainimport "fmt"
type Book struct {title stringauth string
}func main() {var book1 Bookbook1.title = "Golang"book1.auth = "zhang3"fmt.Println("%v\n",book1)
}
- 结构体传参
package mainimport "fmt"
type Book struct {title stringauth string
}func changeBook(book Book) {book.auth = "666"
}func changeBook3(book *Book) {book.auth = "777"}func main() {var book1 Bookbook1.title = "Golang"book1.auth = "zhang3"fmt.Println("%v\n",book1)changeBook(book1)fmt.Println("副本传递",book1)changeBook3(&book1)fmt.Println("指针传递",book1)
}