Compare commits
No commits in common. "bf872f09195a92c31fcd52f8e8fa1a890adcc018" and "354983d93f81fab16da6c09c7945f38a9c8a66cf" have entirely different histories.
bf872f0919
...
354983d93f
@ -1,22 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func intSeq() func() int {
|
|
||||||
i := 0
|
|
||||||
return func() int {
|
|
||||||
i++
|
|
||||||
return i
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
nextInt := intSeq()
|
|
||||||
|
|
||||||
fmt.Println(nextInt())
|
|
||||||
fmt.Println(nextInt())
|
|
||||||
fmt.Println(nextInt())
|
|
||||||
|
|
||||||
newInts := intSeq()
|
|
||||||
fmt.Println(newInts())
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func plus(a int, b int) int {
|
|
||||||
return a+b
|
|
||||||
}
|
|
||||||
|
|
||||||
func plusPlus(a, b, c int) int {
|
|
||||||
return a + b + c
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
res := plus(1, 2)
|
|
||||||
fmt.Println("1+2 =", res)
|
|
||||||
|
|
||||||
res = plusPlus(1, 2, 3)
|
|
||||||
fmt.Println("1+2+3 =", res)
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func vals() (int, int) {
|
|
||||||
return 3,7
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
a,b := vals()
|
|
||||||
fmt.Println(a)
|
|
||||||
fmt.Println(b)
|
|
||||||
|
|
||||||
_, c := vals()
|
|
||||||
fmt.Println(c)
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
nums := []int{2, 3, 4}
|
|
||||||
sum := 0
|
|
||||||
for _, num := range nums {
|
|
||||||
sum += num
|
|
||||||
}
|
|
||||||
fmt.Println("sum:",sum)
|
|
||||||
|
|
||||||
for i,num := range nums {
|
|
||||||
if num == 3 {
|
|
||||||
fmt.Println("index:",i)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
kvs := map[string]string{"a":"apple", "b":"banana"}
|
|
||||||
for k, v := range kvs {
|
|
||||||
fmt.Printf("%s -> %s\n", k, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
for k := range kvs {
|
|
||||||
fmt.Println("key:", k)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i,c := range "go" {
|
|
||||||
fmt.Println(i,c)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func fact(n int) int {
|
|
||||||
if n==0 {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return n * fact(n-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
fmt.Println(fact(7))
|
|
||||||
|
|
||||||
var fib func(n int) int
|
|
||||||
|
|
||||||
fib = func(n int) int {
|
|
||||||
if n<2 {
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
return fib(n-1) + fib(n-2)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(fib(7))
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func sum(nums ...int) {
|
|
||||||
fmt.Print(nums, " ")
|
|
||||||
total := 0
|
|
||||||
for _, num := range nums {
|
|
||||||
total += num
|
|
||||||
}
|
|
||||||
fmt.Println(total)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
sum(1,2)
|
|
||||||
sum(1,2,3)
|
|
||||||
|
|
||||||
nums := []int{1, 2, 3, 4}
|
|
||||||
sum(nums...)
|
|
||||||
}
|
|
@ -1,11 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
"c.infdj.com/GO/courses_golang/hello/morestrings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// fmt.Println("Hello, world.")
|
fmt.Println("Hello, world.")
|
||||||
fmt.Println(morestrings.ReverseRunes("!oG ,olleH"))
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
// Package morestrings implements additional functions to manipulate UTF-8
|
|
||||||
// encoded strings, beyond what is provided in the standard "strings" package.
|
|
||||||
package morestrings
|
|
||||||
|
|
||||||
// ReverseRunes returns its argument string reversed rune-wise left to right.
|
|
||||||
func ReverseRunes(s string) string {
|
|
||||||
r := []rune(s)
|
|
||||||
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
|
|
||||||
r[i], r[j] = r[j], r[i]
|
|
||||||
}
|
|
||||||
return string(r)
|
|
||||||
}
|
|
@ -1,19 +0,0 @@
|
|||||||
package morestrings
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestReverseRunes(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
in, want string
|
|
||||||
}{
|
|
||||||
{"Hello, world", "dlrow ,olleH"},
|
|
||||||
{"Hello, 世界", "界世 ,olleH"},
|
|
||||||
{"", ""},
|
|
||||||
}
|
|
||||||
for _, c := range cases {
|
|
||||||
got := ReverseRunes(c.in)
|
|
||||||
if got != c.want {
|
|
||||||
t.Errorf("ReverseRunes(%q) == %q, want %q", c.in, got, c.want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user