add: quiz and exercises to pointers
This commit is contained in:
29
26-pointers/exercises/02-swap/solution/main.go
Normal file
29
26-pointers/exercises/02-swap/solution/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a, b := 3.14, 6.28
|
||||
swap(&a, &b)
|
||||
fmt.Printf("a : %g — b : %g\n", a, b)
|
||||
|
||||
pa, pb := &a, &b
|
||||
pa, pb = swapAddr(pa, pb)
|
||||
fmt.Printf("pa: %p — pb: %p\n", pa, pb)
|
||||
fmt.Printf("pa: %g — pb: %g\n", *pa, *pb)
|
||||
}
|
||||
|
||||
func swap(a, b *float64) {
|
||||
*a, *b = *b, *a
|
||||
}
|
||||
|
||||
func swapAddr(a, b *float64) (*float64, *float64) {
|
||||
return b, a
|
||||
}
|
Reference in New Issue
Block a user