Compare commits
5 Commits
354983d93f
...
master
Author | SHA1 | Date | |
---|---|---|---|
742bbc6e1e | |||
5160e2ae78 | |||
bf872f0919 | |||
dbc1292105 | |||
b0a83f934e |
12
gobyexample/channel-buffering.go
Normal file
12
gobyexample/channel-buffering.go
Normal file
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
messages := make(chan string, 2)
|
||||
messages <- "buffered"
|
||||
messages <- "channel"
|
||||
|
||||
fmt.Println(<-messages)
|
||||
fmt.Println(<-messages)
|
||||
}
|
20
gobyexample/channel-directions.go
Normal file
20
gobyexample/channel-directions.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func ping(pings chan<- string, msg string) {
|
||||
pings <- msg
|
||||
}
|
||||
|
||||
func pong(pings <-chan string, pongs chan<- string) {
|
||||
msg := <-pings
|
||||
pongs <- msg
|
||||
}
|
||||
|
||||
func main() {
|
||||
pings := make(chan string, 1)
|
||||
pongs := make(chan string, 1)
|
||||
ping(pings, "passed message")
|
||||
pong(pings, pongs)
|
||||
fmt.Println(<-pongs)
|
||||
}
|
21
gobyexample/channel-synchronization.go
Normal file
21
gobyexample/channel-synchronization.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func worker(done chan bool) {
|
||||
fmt.Print("working...")
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println("done")
|
||||
|
||||
done <- true
|
||||
}
|
||||
|
||||
func main() {
|
||||
done := make(chan bool, 1)
|
||||
go worker(done)
|
||||
|
||||
<-done
|
||||
}
|
12
gobyexample/channels.go
Normal file
12
gobyexample/channels.go
Normal file
@ -0,0 +1,12 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
messages := make(chan string)
|
||||
|
||||
go func() { messages <- "ping" }()
|
||||
|
||||
msg := <-messages
|
||||
fmt.Println(msg)
|
||||
}
|
22
gobyexample/closures.go
Normal file
22
gobyexample/closures.go
Normal file
@ -0,0 +1,22 @@
|
||||
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())
|
||||
}
|
53
gobyexample/errors.go
Normal file
53
gobyexample/errors.go
Normal file
@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func f1(arg int) (int, error) {
|
||||
if arg == 42 {
|
||||
return -1, errors.New("can't work with 42")
|
||||
}
|
||||
return arg + 3, nil
|
||||
}
|
||||
|
||||
type argError struct {
|
||||
arg int
|
||||
prob string
|
||||
}
|
||||
|
||||
func (e *argError) Error() string {
|
||||
return fmt.Sprintf("%d - %s", e.arg, e.prob)
|
||||
}
|
||||
|
||||
func f2(arg int) (int, error) {
|
||||
if arg == 42 {
|
||||
return -1, &argError{arg, "can't work with it"}
|
||||
}
|
||||
return arg + 3, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
for _, i := range []int{7 ,42} {
|
||||
if r, e := f1(i); e != nil {
|
||||
fmt.Println("f1 failed:", e)
|
||||
} else {
|
||||
fmt.Println("f1 worked:", r)
|
||||
}
|
||||
}
|
||||
|
||||
for _, i := range []int{7, 42} {
|
||||
if r,e := f2(i); e != nil {
|
||||
fmt.Println("f2 failed:", e)
|
||||
} else {
|
||||
fmt.Println("f2 worked:", r)
|
||||
}
|
||||
}
|
||||
|
||||
_, e := f2(42)
|
||||
if ae, ok := e.(*argError); ok {
|
||||
fmt.Println(ae.arg)
|
||||
fmt.Println(ae.prob)
|
||||
}
|
||||
}
|
19
gobyexample/functions.go
Normal file
19
gobyexample/functions.go
Normal file
@ -0,0 +1,19 @@
|
||||
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)
|
||||
}
|
25
gobyexample/goroutines.go
Normal file
25
gobyexample/goroutines.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func f(from string) {
|
||||
for i := 0; i < 3; i++ {
|
||||
fmt.Println(from, ":", i)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
f("direct")
|
||||
|
||||
go f("goroutine")
|
||||
|
||||
go func(msg string) {
|
||||
fmt.Println(msg)
|
||||
}("going")
|
||||
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println("done")
|
||||
}
|
46
gobyexample/interfaces.go
Normal file
46
gobyexample/interfaces.go
Normal file
@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type geometry interface {
|
||||
area() float64
|
||||
perim() float64
|
||||
}
|
||||
|
||||
type rect struct {
|
||||
width, height float64
|
||||
}
|
||||
type circle struct {
|
||||
radius float64
|
||||
}
|
||||
|
||||
func (r *rect) area() float64 {
|
||||
return r.width * r.height
|
||||
}
|
||||
func (r *rect) perim() float64 {
|
||||
return 2*r.width + 2*r.height
|
||||
}
|
||||
|
||||
func (c *circle) area() float64 {
|
||||
return math.Pi * c.radius * c.radius
|
||||
}
|
||||
func (c *circle) perim() float64 {
|
||||
return 2 * math.Pi * c.radius
|
||||
}
|
||||
|
||||
func measure(g geometry) {
|
||||
fmt.Println(g)
|
||||
fmt.Println(g.area())
|
||||
fmt.Println(g.perim())
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := &rect{width:3, height:4}
|
||||
c := &circle{radius: 5}
|
||||
|
||||
measure(r)
|
||||
measure(c)
|
||||
}
|
26
gobyexample/methods.go
Normal file
26
gobyexample/methods.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type rect struct {
|
||||
width, height int
|
||||
}
|
||||
|
||||
func (r *rect) area() int {
|
||||
return r.width * r.height
|
||||
}
|
||||
|
||||
func (r rect) perim() int {
|
||||
return 2*r.width + 2*r.height
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := rect{width: 10, height: 5}
|
||||
|
||||
fmt.Println("area:", r.area())
|
||||
fmt.Println("perim:", r.perim())
|
||||
|
||||
rp := &r
|
||||
fmt.Println("area:", rp.area())
|
||||
fmt.Println("perim:", rp.perim())
|
||||
}
|
16
gobyexample/multiple-return-values.go
Normal file
16
gobyexample/multiple-return-values.go
Normal file
@ -0,0 +1,16 @@
|
||||
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)
|
||||
}
|
24
gobyexample/pointers.go
Normal file
24
gobyexample/pointers.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func zeroval(ival int) {
|
||||
ival = 0
|
||||
}
|
||||
|
||||
func zeroptr(iptr *int) {
|
||||
*iptr = 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
i := 1
|
||||
fmt.Println("initial:", i)
|
||||
|
||||
zeroval(i)
|
||||
fmt.Println("zeroval:", i)
|
||||
|
||||
zeroptr(&i)
|
||||
fmt.Println("zeroptr:", i)
|
||||
|
||||
fmt.Println("pointer:", &i)
|
||||
}
|
31
gobyexample/range.go
Normal file
31
gobyexample/range.go
Normal file
@ -0,0 +1,31 @@
|
||||
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)
|
||||
}
|
||||
}
|
25
gobyexample/recursion.go
Normal file
25
gobyexample/recursion.go
Normal file
@ -0,0 +1,25 @@
|
||||
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))
|
||||
}
|
30
gobyexample/select.go
Normal file
30
gobyexample/select.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
c1 := make(chan string)
|
||||
c2 := make(chan string)
|
||||
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
c1 <- "one"
|
||||
}()
|
||||
|
||||
go func() {
|
||||
time.Sleep(2 * time.Second)
|
||||
c1 <- "two"
|
||||
}()
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
select {
|
||||
case msg1 := <-c1:
|
||||
fmt.Println("received", msg1)
|
||||
case msg2 := <-c2:
|
||||
fmt.Println("received", msg2)
|
||||
}
|
||||
}
|
||||
}
|
38
gobyexample/structs.go
Normal file
38
gobyexample/structs.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type person struct {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
func newPerson(name string) *person {
|
||||
p := person{name:name}
|
||||
p.age = 42
|
||||
return &p
|
||||
}
|
||||
|
||||
|
||||
func main() {
|
||||
|
||||
fmt.Println(person{"Bob", 20})
|
||||
|
||||
fmt.Println(person{name:"Alice", age:30})
|
||||
|
||||
fmt.Println(person{name:"Fred"})
|
||||
|
||||
fmt.Println(&person{name: "Ann", age: 40})
|
||||
|
||||
fmt.Println(newPerson("Jon"))
|
||||
|
||||
s := person{name:"Sean", age: 50}
|
||||
fmt.Println(s.name)
|
||||
|
||||
sp := &s
|
||||
fmt.Println(sp.age)
|
||||
|
||||
sp.age = 51
|
||||
fmt.Println(sp.age)
|
||||
|
||||
}
|
20
gobyexample/variadic-functions.go
Normal file
20
gobyexample/variadic-functions.go
Normal file
@ -0,0 +1,20 @@
|
||||
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...)
|
||||
}
|
29
gobyexample/waitgroups.go
Normal file
29
gobyexample/waitgroups.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func worker(id int) {
|
||||
fmt.Printf("Worker %d starting\n", id)
|
||||
time.Sleep(time.Second)
|
||||
fmt.Printf("Worker %d done\n", id)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for i:= 1; i<=5; i++ {
|
||||
wg.Add(1)
|
||||
|
||||
i := i
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
worker(i)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
@ -1,7 +1,11 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"c.infdj.com/GO/courses_golang/hello/morestrings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, world.")
|
||||
// fmt.Println("Hello, world.")
|
||||
fmt.Println(morestrings.ReverseRunes("!oG ,olleH"))
|
||||
}
|
||||
|
12
hello/morestrings/reverse.go
Normal file
12
hello/morestrings/reverse.go
Normal file
@ -0,0 +1,12 @@
|
||||
// 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)
|
||||
}
|
19
hello/morestrings/reverse_test.go
Normal file
19
hello/morestrings/reverse_test.go
Normal file
@ -0,0 +1,19 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user