Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
11
Task/Call-a-function/Go/call-a-function-1.go
Normal file
11
Task/Call-a-function/Go/call-a-function-1.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import (
|
||||
"image"
|
||||
"image/gif"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
func f() (int, float64) { return 0, 0 }
|
||||
func g(int, float64) int { return 0 }
|
||||
func h(string, ...int) {}
|
||||
2
Task/Call-a-function/Go/call-a-function-10.go
Normal file
2
Task/Call-a-function/Go/call-a-function-10.go
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
list = append(list, a, d, e, i)
|
||||
i = len(list)
|
||||
21
Task/Call-a-function/Go/call-a-function-11.go
Normal file
21
Task/Call-a-function/Go/call-a-function-11.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
// int parameter, so arguments will be passed to it by value.
|
||||
func zeroval(ival int) {
|
||||
ival = 0
|
||||
}
|
||||
// has an *int parameter, meaning that it takes an int pointer.
|
||||
func zeroptr(iptr *int) {
|
||||
*iptr = 0
|
||||
}
|
||||
func main() {
|
||||
i := 1
|
||||
fmt.Println("initial:", i) // prt initial: 1
|
||||
zeroval(i)
|
||||
fmt.Println("zeroval:", i) // prt zeroval: 1
|
||||
zeroptr(&i)
|
||||
fmt.Println("zeroptr:", i) // prt zeroptr: 0
|
||||
fmt.Println("pointer:", &i) // prt pointer: 0xc0000140b8
|
||||
}
|
||||
27
Task/Call-a-function/Go/call-a-function-12.go
Normal file
27
Task/Call-a-function/Go/call-a-function-12.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func mkAdd(a int) func(int) int {
|
||||
return func(b int) int {
|
||||
return a + b
|
||||
}
|
||||
}
|
||||
func sum(x, y int) int {
|
||||
return x + y
|
||||
}
|
||||
|
||||
func partialSum(x int) func(int) int {
|
||||
return func(y int) int {
|
||||
return sum(x, y)
|
||||
}
|
||||
}
|
||||
func main() {
|
||||
// Is partial application possible and how
|
||||
add2 := mkAdd(2)
|
||||
add3 := mkAdd(3)
|
||||
fmt.Println(add2(5), add3(6)) // prt 7 9
|
||||
// Currying functions in go
|
||||
partial := partialSum(13)
|
||||
fmt.Println(partial(5)) //prt 18
|
||||
}
|
||||
9
Task/Call-a-function/Go/call-a-function-2.go
Normal file
9
Task/Call-a-function/Go/call-a-function-2.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
f()
|
||||
g(1, 2.0)
|
||||
// If f() is defined to return exactly the number and type of
|
||||
// arguments that g() accepts than they can be used in place:
|
||||
g(f())
|
||||
// But only without other arguments, this won't compile:
|
||||
//h("fail", f())
|
||||
// But this will:
|
||||
g(g(1, 2.0), 3.0)
|
||||
8
Task/Call-a-function/Go/call-a-function-3.go
Normal file
8
Task/Call-a-function/Go/call-a-function-3.go
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
h("ex1")
|
||||
h("ex2", 1, 2)
|
||||
h("ex3", 1, 2, 3, 4)
|
||||
// such functions can also be called by expanding a slice:
|
||||
list := []int{1,2,3,4}
|
||||
h("ex4", list...)
|
||||
// but again, not mixed with other arguments, this won't compile:
|
||||
//h("fail", 2, list...)
|
||||
1
Task/Call-a-function/Go/call-a-function-4.go
Normal file
1
Task/Call-a-function/Go/call-a-function-4.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
gif.Encode(ioutil.Discard, image.Black, &gif.Options{NumColors: 16})
|
||||
14
Task/Call-a-function/Go/call-a-function-5.go
Normal file
14
Task/Call-a-function/Go/call-a-function-5.go
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Params struct {
|
||||
a, b, c int
|
||||
}
|
||||
func doIt(p Params) int {
|
||||
return p.a + p.b + p.c
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(doIt(Params{a: 1, c: 9})) // prt 10
|
||||
}
|
||||
15
Task/Call-a-function/Go/call-a-function-6.go
Normal file
15
Task/Call-a-function/Go/call-a-function-6.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func bar(a, b, c int) {
|
||||
fmt.Printf("%d, %d, %d", a, b, c)
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := make(map[string]int)
|
||||
args["a"] = 3
|
||||
args["b"] = 2
|
||||
args["c"] = 1
|
||||
bar(args["a"], args["b"], args["c"]) // prt 3, 2, 1
|
||||
}
|
||||
1
Task/Call-a-function/Go/call-a-function-7.go
Normal file
1
Task/Call-a-function/Go/call-a-function-7.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
if 2*g(1, 3.0)+4 > 0 {}
|
||||
9
Task/Call-a-function/Go/call-a-function-8.go
Normal file
9
Task/Call-a-function/Go/call-a-function-8.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
fn := func(r rune) rune {
|
||||
if unicode.IsSpace(r) {
|
||||
return -1
|
||||
}
|
||||
return r
|
||||
}
|
||||
strings.Map(fn, "Spaces removed")
|
||||
strings.Map(unicode.ToLower, "Test")
|
||||
strings.Map(func(r rune) rune { return r + 1 }, "shift")
|
||||
4
Task/Call-a-function/Go/call-a-function-9.go
Normal file
4
Task/Call-a-function/Go/call-a-function-9.go
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
a, b := f() // multivalue return
|
||||
_, c := f() // only some of a multivalue return
|
||||
d := g(a, c) // single return value
|
||||
e, i := g(d, b), g(d, 2) // multiple assignment
|
||||
Loading…
Add table
Add a link
Reference in a new issue