Skip to content

Instantly share code, notes, and snippets.

@wnyao
Forked from josephspurrier/values_pointers.go
Created November 6, 2022 19:19
Show Gist options
  • Save wnyao/9e9d85d07b555617e7a0bc54abf97327 to your computer and use it in GitHub Desktop.
Save wnyao/9e9d85d07b555617e7a0bc54abf97327 to your computer and use it in GitHub Desktop.

Revisions

  1. @josephspurrier josephspurrier revised this gist Aug 21, 2016. 1 changed file with 19 additions and 19 deletions.
    38 changes: 19 additions & 19 deletions values_pointers.go
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@ Also available at: https://play.golang.org/p/lNpnS9j1ma
    Allowed:
    --------
    p := Person{"Hillary", 28} stores the value
    p := &Person{"Hillary", 28} stores the pointer address (reference)
    p := Person{"Steve", 28} stores the value
    p := &Person{"Steve", 28} stores the pointer address (reference)
    PrintPerson(p) passes either the value or pointer address (reference)
    PrintPerson(*p) passes the value
    PrintPerson(&p) passes the pointer address (reference)
    @@ -17,7 +17,7 @@ func PrintPerson(p *Person) ONLY receives the pointer address (reference)
    Not Allowed:
    --------
    p := *Person{"Hillary", 28} illegal
    p := *Person{"Steve", 28} illegal
    func PrintPerson(p &Person) illegal
    */
    @@ -51,7 +51,7 @@ func (p *Person) String() string {
    // *****************************************************************************

    func test1() {
    p := Person{"Hillary", 28}
    p := Person{"Steve", 28}
    printPerson1(p)
    updatePerson1(p)
    printPerson1(p)
    @@ -74,7 +74,7 @@ func printPerson1(p Person) {
    // *****************************************************************************

    func test2() {
    p := &Person{"Hillary", 28}
    p := &Person{"Steve", 28}
    printPerson2(p)
    updatePerson2(p)
    printPerson2(p)
    @@ -97,7 +97,7 @@ func printPerson2(p *Person) {
    // *****************************************************************************

    func test3() {
    p := Person{"Hillary", 28}
    p := Person{"Steve", 28}
    printPerson3(&p)
    updatePerson3(&p)
    printPerson3(&p)
    @@ -120,7 +120,7 @@ func printPerson3(p *Person) {
    // *****************************************************************************

    func test4() {
    p := &Person{"Hillary", 28}
    p := &Person{"Steve", 28}
    printPerson4(*p)
    updatePerson4(*p)
    printPerson4(*p)
    @@ -144,18 +144,18 @@ func printPerson4(p Person) {

    /*
    Outputs:
    String: {Hillary 28} | Name: Hillary | Age: 28
    String: {Hillary 32} | Name: Hillary | Age: 32
    String: {Hillary 28} | Name: Hillary | Age: 28
    String: Hillary is 28 | Name: Hillary | Age: 28
    String: Hillary is 32 | Name: Hillary | Age: 32
    String: Hillary is 32 | Name: Hillary | Age: 32
    String: Hillary is 28 | Name: Hillary | Age: 28
    String: Hillary is 32 | Name: Hillary | Age: 32
    String: Hillary is 32 | Name: Hillary | Age: 32
    String: {Hillary 28} | Name: Hillary | Age: 28
    String: {Hillary 32} | Name: Hillary | Age: 32
    String: {Hillary 28} | Name: Hillary | Age: 28
    String: {Steve 28} | Name: Steve | Age: 28
    String: {Steve 32} | Name: Steve | Age: 32
    String: {Steve 28} | Name: Steve | Age: 28
    String: Steve is 28 | Name: Steve | Age: 28
    String: Steve is 32 | Name: Steve | Age: 32
    String: Steve is 32 | Name: Steve | Age: 32
    String: Steve is 28 | Name: Steve | Age: 28
    String: Steve is 32 | Name: Steve | Age: 32
    String: Steve is 32 | Name: Steve | Age: 32
    String: {Steve 28} | Name: Steve | Age: 28
    String: {Steve 32} | Name: Steve | Age: 32
    String: {Steve 28} | Name: Steve | Age: 28
    */
    func main() {
    test1()
  2. @josephspurrier josephspurrier revised this gist Jan 1, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion values_pointers.go
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    Golang - Asterisk and Ampersand Cheatsheet
    ********************************************************************************
    Also available at: [https://play.golang.org/p/lNpnS9j1ma](https://play.golang.org/p/lNpnS9j1ma)
    Also available at: https://play.golang.org/p/lNpnS9j1ma
    Allowed:
    --------
  3. @josephspurrier josephspurrier revised this gist Jan 1, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion values_pointers.go
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    Golang - Asterisk and Ampersand Cheatsheet
    ********************************************************************************
    Also available at: https://play.golang.org/p/lNpnS9j1ma
    Also available at: [https://play.golang.org/p/lNpnS9j1ma](https://play.golang.org/p/lNpnS9j1ma)
    Allowed:
    --------
  4. @josephspurrier josephspurrier revised this gist Jan 1, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions values_pointers.go
    Original file line number Diff line number Diff line change
    @@ -8,16 +8,16 @@ Also available at: https://play.golang.org/p/lNpnS9j1ma
    Allowed:
    --------
    p := Person{"Hillary", 28} stores the value
    p := &Person{"Hillary", 28} stores the pointer address (reference)
    PrintPerson(p) passes either the value or pointer address (reference)
    PrintPerson(*p) passes the value
    PrintPerson(&p) passes the pointer address (reference)
    p := &Person{"Hillary", 28} stores the pointer address (reference)
    PrintPerson(p) passes either the value or pointer address (reference)
    PrintPerson(*p) passes the value
    PrintPerson(&p) passes the pointer address (reference)
    func PrintPerson(p Person) ONLY receives the value
    func PrintPerson(p *Person) ONLY receives the pointer address (reference)
    Not Allowed:
    --------
    p := *Person{"Hillary", 28} illegal
    p := *Person{"Hillary", 28} illegal
    func PrintPerson(p &Person) illegal
    */
  5. @josephspurrier josephspurrier created this gist Jan 1, 2015.
    165 changes: 165 additions & 0 deletions values_pointers.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,165 @@
    /*
    ********************************************************************************
    Golang - Asterisk and Ampersand Cheatsheet
    ********************************************************************************
    Also available at: https://play.golang.org/p/lNpnS9j1ma
    Allowed:
    --------
    p := Person{"Hillary", 28} stores the value
    p := &Person{"Hillary", 28} stores the pointer address (reference)
    PrintPerson(p) passes either the value or pointer address (reference)
    PrintPerson(*p) passes the value
    PrintPerson(&p) passes the pointer address (reference)
    func PrintPerson(p Person) ONLY receives the value
    func PrintPerson(p *Person) ONLY receives the pointer address (reference)
    Not Allowed:
    --------
    p := *Person{"Hillary", 28} illegal
    func PrintPerson(p &Person) illegal
    */

    package main

    import (
    "fmt"
    )

    type Person struct {
    Name string
    Age int
    }

    // This only works with *Person, does not work with Person
    // Only works with Test 2 and Test 3
    func (p *Person) String() string {
    return fmt.Sprintf("%s is %d", p.Name, p.Age)
    }

    // This works with both *Person and Person, BUT you can't modiy the value and
    // it takes up more space
    // Works with Test 1, Test 2, Test 3, and Test 4
    /*func (p Person) String() string {
    return fmt.Sprintf("%s is %d", p.Name, p.Age)
    }*/

    // *****************************************************************************
    // Test 1 - Pass by Value
    // *****************************************************************************

    func test1() {
    p := Person{"Hillary", 28}
    printPerson1(p)
    updatePerson1(p)
    printPerson1(p)
    }

    func updatePerson1(p Person) {
    p.Age = 32
    printPerson1(p)
    }

    func printPerson1(p Person) {
    fmt.Printf("String: %v | Name: %v | Age: %d\n",
    p,
    p.Name,
    p.Age)
    }

    // *****************************************************************************
    // Test 2 - Pass by Reference
    // *****************************************************************************

    func test2() {
    p := &Person{"Hillary", 28}
    printPerson2(p)
    updatePerson2(p)
    printPerson2(p)
    }

    func updatePerson2(p *Person) {
    p.Age = 32
    printPerson2(p)
    }

    func printPerson2(p *Person) {
    fmt.Printf("String: %v | Name: %v | Age: %d\n",
    p,
    p.Name,
    p.Age)
    }

    // *****************************************************************************
    // Test 3 - Pass by Reference (requires more typing)
    // *****************************************************************************

    func test3() {
    p := Person{"Hillary", 28}
    printPerson3(&p)
    updatePerson3(&p)
    printPerson3(&p)
    }

    func updatePerson3(p *Person) {
    p.Age = 32
    printPerson3(p)
    }

    func printPerson3(p *Person) {
    fmt.Printf("String: %v | Name: %v | Age: %d\n",
    p,
    p.Name,
    p.Age)
    }

    // *****************************************************************************
    // Test 4 - Pass by Value (requires more typing)
    // *****************************************************************************

    func test4() {
    p := &Person{"Hillary", 28}
    printPerson4(*p)
    updatePerson4(*p)
    printPerson4(*p)
    }

    func updatePerson4(p Person) {
    p.Age = 32
    printPerson4(p)
    }

    func printPerson4(p Person) {
    fmt.Printf("String: %v | Name: %v | Age: %d\n",
    p,
    p.Name,
    p.Age)
    }

    // *****************************************************************************
    // Main
    // *****************************************************************************

    /*
    Outputs:
    String: {Hillary 28} | Name: Hillary | Age: 28
    String: {Hillary 32} | Name: Hillary | Age: 32
    String: {Hillary 28} | Name: Hillary | Age: 28
    String: Hillary is 28 | Name: Hillary | Age: 28
    String: Hillary is 32 | Name: Hillary | Age: 32
    String: Hillary is 32 | Name: Hillary | Age: 32
    String: Hillary is 28 | Name: Hillary | Age: 28
    String: Hillary is 32 | Name: Hillary | Age: 32
    String: Hillary is 32 | Name: Hillary | Age: 32
    String: {Hillary 28} | Name: Hillary | Age: 28
    String: {Hillary 32} | Name: Hillary | Age: 32
    String: {Hillary 28} | Name: Hillary | Age: 28
    */
    func main() {
    test1()
    test2()
    test3()
    test4()
    }