-
-
Save wnyao/9e9d85d07b555617e7a0bc54abf97327 to your computer and use it in GitHub Desktop.
Revisions
-
josephspurrier revised this gist
Aug 21, 2016 . 1 changed file with 19 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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{"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{"Steve", 28} illegal func PrintPerson(p &Person) illegal */ @@ -51,7 +51,7 @@ func (p *Person) String() string { // ***************************************************************************** func test1() { p := Person{"Steve", 28} printPerson1(p) updatePerson1(p) printPerson1(p) @@ -74,7 +74,7 @@ func printPerson1(p Person) { // ***************************************************************************** func test2() { p := &Person{"Steve", 28} printPerson2(p) updatePerson2(p) printPerson2(p) @@ -97,7 +97,7 @@ func printPerson2(p *Person) { // ***************************************************************************** func test3() { p := Person{"Steve", 28} printPerson3(&p) updatePerson3(&p) printPerson3(&p) @@ -120,7 +120,7 @@ func printPerson3(p *Person) { // ***************************************************************************** func test4() { p := &Person{"Steve", 28} printPerson4(*p) updatePerson4(*p) printPerson4(*p) @@ -144,18 +144,18 @@ func printPerson4(p Person) { /* Outputs: 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() -
josephspurrier revised this gist
Jan 1, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 Allowed: -------- -
josephspurrier revised this gist
Jan 1, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) Allowed: -------- -
josephspurrier revised this gist
Jan 1, 2015 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) 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 */ -
josephspurrier created this gist
Jan 1, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() }