go 指针

package main

import (
    "fmt"
)

type Txt struct {
    name string
}

func main() {

    //list1-------------------------
    x := 1
    y := 2

    fmt.Println("x1:", x)
    fmt.Println("y1:", y)
    GetF(&x, &y)
    fmt.Println("x2:", x)
    fmt.Println("y2:", y)
    //list2--------------------------
    sex := "nv"
    age := 18
    t := new(Txt)

    fmt.Println("sex1", sex)
    fmt.Println("age1", age)

    t.Context(&sex, &age)

    fmt.Println("sex3", sex)
    fmt.Println("age2", age)

    t.name = "tname"
    fmt.Println(t.name)
    //list GetE------------------------
    x1 := 2
    y1 := 1
    fmt.Println("x1-1", x1)
    fmt.Println("y1-1", y1)
    GetE(&x1, &y1)
    fmt.Println("x1-1", x1)
    fmt.Println("y1-1", y1)

}

func (c *Txt) Context(sex *string, age *int) {
    sexs := "black"
    sex = &sexs
    ages := 21
    age = &ages

    fmt.Println("sex2", sex)
    fmt.Println("age2", age)

}

func GetF(x, y *int) {
    var r int

    r = *x
    *x = *y
    *y = r
}

func GetE(x1, y1 *int) {
    *x1, *y1 = *y1, *x1
}

 

posted @ 2017-02-14 13:58  阿波罗一号  阅读(132)  评论(0编辑  收藏  举报