1. package main
    2. import "fmt"
    3. //int别名
    4. type myint int
    5. //结构体
    6. type Book struct {
    7. title string
    8. auth string
    9. }
    10. func change(book Book) {
    11. //传递副本
    12. book.title="java"
    13. }
    14. func changeBook(book *Book) {
    15. //传递指针
    16. book.title="java"
    17. }
    18. func main() {
    19. var a myint = 10
    20. fmt.Println(a)
    21. fmt.Printf("%T\n",a)
    22. var book1 Book
    23. book1.auth="zhangsan"
    24. book1.title="Golang"
    25. fmt.Println(book1)
    26. change(book1)
    27. fmt.Println(book1)
    28. //传递地址
    29. changeBook(&book1)
    30. fmt.Println(book1)
    31. }
    1. package main
    2. import "fmt"
    3. //类首字母大写,表示其他包能够访问
    4. type Hero struct {
    5. //如果类属性大写,表示属性是对外能够访问
    6. Name string
    7. Ad int
    8. Level int
    9. }
    10. /*
    11. func (this Hero) Show(){
    12. fmt.Println(this.Name,this.Ad,this.Level)
    13. }
    14. func (this Hero) GetName() string {
    15. return this.Name
    16. }
    17. func (this Hero) SetName(newName string){
    18. this.Name=newName
    19. }
    20. */
    21. func (this *Hero) Show(){
    22. fmt.Println(this.Name,this.Ad,this.Level)
    23. }
    24. func (this *Hero) GetName() string {
    25. return this.Name
    26. }
    27. func (this *Hero) SetName(newName string){
    28. this.Name=newName
    29. }
    30. func main() {
    31. hero:=Hero{
    32. Name: "zhangsan",
    33. Ad:110,
    34. Level:1}
    35. hero.Show()
    36. hero.SetName("lisi")
    37. hero.Show()
    38. }