zoukankan      html  css  js  c++  java
  • GO 结构体

    结构体是作为参数的值传递:

    package main
    
    import "fmt"
    
    type Books struct {
        title string
        author string
        subject string
        book_id int
    }
    
    func changeBook(book Books) {
        book.title = "book1_change"
    }
    
    func main() {
        var book1 Books;
        book1.title = "book1"
        book1.author = "zuozhe"
        book1.book_id = 1
        changeBook(book1)
        fmt.Println(book1)
    }

    结果为:

    {book1 zuozhe  1}

    如果想在函数里面改变结果体数据内容,需要传入指针:

    package main
    
    import "fmt"
    
    type Books struct {
        title string
        author string
        subject string
        book_id int
    }
    
    func changeBook(book *Books) {
        book.title = "book1_change"
    }
    
    func main() {
        var book1 Books;
        book1.title = "book1"
        book1.author = "zuozhe"
        book1.book_id = 1
        changeBook(&book1)
        fmt.Println(book1)
    }
  • 相关阅读:
    Car HDU
    Defeat the Enemy UVALive
    Alice and Bob HDU
    Gone Fishing POJ
    Radar Installation POJ
    Supermarket POJ
    Moo Volume POJ
    Text Document Analysis CodeForces
    checkbox全选与反选

  • 原文地址:https://www.cnblogs.com/justart/p/11662189.html
Copyright © 2011-2022 走看看