如下所示 两个文件,打印的时候 传递指针变量 效果相同
如果需要修改。只能传入指针才可以修改
/* main.go */ package main type Books struct { title string } func main() { var Book1 Books var Book2 Books Book1.title = "Go 语言" Book2.title = "Python 教程" Set(Book1) Set2(&Book2) PrintBook(Book1) PrintBook(Book2) PrintBook2(&Book1) PrintBook2(&Book2) } /* Book title : Go 语言 Book title : chenggong Book title : Go 语言 Book title : chenggong */ /* change.go */ package main import ( "fmt" ) func Set(book Books){ book.title = "chenggong" } func Set2(book *Books) { book.title="chenggong" } func PrintBook( book Books) { fmt.Printf( "Book title : %s\n", book.title) } func PrintBook2( book *Books) { fmt.Printf( "Book title : %s\n", book.title) }