Go 结构体
?
?1.
package mainimport "fmt"type Books struct {title stringauthor stringsubject stringbook_id int }func main() {// 創建一個新的結構體fmt.Println(Books{"Go 語言", "www.runoob.com", "Go 語言教程", 6495407})// 也可以使用 key => value 格式fmt.Println(Books{title: "Go 語言", author: "www.runoob.com", subject: "Go 語言教程", book_id: 6495407})// 忽略的字段為 0 或 空fmt.Println(Books{title: "Go 語言", author: "www.runoob.com"}) }輸出
{Go 語言 www.runoob.com Go 語言教程 6495407} {Go 語言 www.runoob.com Go 語言教程 6495407} {Go 語言 www.runoob.com 0}?
2.
package mainimport "fmt"type Books struct {title stringauthor stringsubject stringbook_id int }func main() {var Book1 Books /* 聲明 Book1 為 Books 類型 */var Book2 Books /* 聲明 Book2 為 Books 類型 *//* book 1 描述 */Book1.title = "Go 語言"Book1.author = "www.runoob.com"Book1.subject = "Go 語言教程"Book1.book_id = 6495407/* book 2 描述 */Book2.title = "Python 教程"Book2.author = "www.runoob.com"Book2.subject = "Python 語言教程"Book2.book_id = 6495700/* 打印 Book1 信息 */fmt.Printf("Book 1 title : %s\n", Book1.title)fmt.Printf("Book 1 author : %s\n", Book1.author)fmt.Printf("Book 1 subject : %s\n", Book1.subject)fmt.Printf("Book 1 book_id : %d\n", Book1.book_id)/* 打印 Book2 信息 */fmt.Printf("Book 2 title : %s\n", Book2.title)fmt.Printf("Book 2 author : %s\n", Book2.author)fmt.Printf("Book 2 subject : %s\n", Book2.subject)fmt.Printf("Book 2 book_id : %d\n", Book2.book_id) }輸出
Book 1 title : Go 語言 Book 1 author : www.runoob.com Book 1 subject : Go 語言教程 Book 1 book_id : 6495407 Book 2 title : Python 教程 Book 2 author : www.runoob.com Book 2 subject : Python 語言教程 Book 2 book_id : 6495700?
?
轉載于:https://www.cnblogs.com/sea-stream/p/10333255.html
總結
- 上一篇: 十大经典数据挖掘算法之k-means
- 下一篇: PayPal 对接