├── README.md ├── num.go ├── nil.go ├── float.go ├── raw.go ├── pi.go ├── city.go ├── strint.go ├── empty_map.go ├── pi_var.go ├── multi_assign.go ├── strint_conv.go ├── two_cities.go ├── append.go ├── cityu.go ├── flag.go ├── strint_byte.go ├── iota.go ├── init.go ├── next_id.go ├── time.go ├── chan.go ├── runes.go ├── struct_pt.go ├── flag_ok.go ├── chan_empty.go ├── strint_map.go ├── two_cities_nfc.go ├── struct.go ├── count.go ├── sleep_sort.go ├── time_eq.go ├── json_float.go ├── range.go ├── sleep_sort_param.go ├── count_mu.go ├── sleep_sort_scope.go ├── count_atomic.go ├── range_close.go ├── runes_lit.go ├── error.go ├── iota_check.go ├── range_rcv.go ├── job.go ├── job_ptr.go ├── append_impl.go ├── iota_str.go ├── range_ctx.go └── num_lit.go /README.md: -------------------------------------------------------------------------------- 1 | # Go Brain Teasers Code 2 | 3 | This is the code from [Go Brain Teasers][gbt], it is intended for your personal 4 | use while working working with the book. 5 | 6 | The code is copyrighted under [Apache License, Version 2.0][apl2], please 7 | respect it. 8 | 9 | You can also get Kindle or dead tree edition from [Amazon][amz] 10 | 11 | --- 12 | Enjoy, Miki 13 | 14 | [amz]: https://www.amazon.com/dp/B0876DBMFM/ 15 | [apl2]: https://opensource.org/licenses/Apache-2.0 16 | [gbt]: https://gum.co/Qkmou 17 | -------------------------------------------------------------------------------- /num.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | fmt.Println(0x1p-2) 25 | } 26 | -------------------------------------------------------------------------------- /nil.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | n := nil 25 | fmt.Println(n) 26 | } 27 | -------------------------------------------------------------------------------- /float.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | n := 1.1 25 | fmt.Println(n * n) 26 | } 27 | -------------------------------------------------------------------------------- /raw.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | s := `a\tb` 25 | fmt.Println(s) 26 | } 27 | -------------------------------------------------------------------------------- /pi.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | var π = 22 / 7.0 25 | fmt.Println(π) 26 | } 27 | -------------------------------------------------------------------------------- /city.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | city := "Kraków" 25 | fmt.Println(len(city)) 26 | } 27 | -------------------------------------------------------------------------------- /strint.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | i := 169 25 | s := string(i) 26 | fmt.Println(s) 27 | } 28 | -------------------------------------------------------------------------------- /empty_map.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | var m map[string]int 25 | fmt.Println(m["errors"]) 26 | } 27 | -------------------------------------------------------------------------------- /pi_var.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | a, b := 22, 7.0 25 | var π = a / b 26 | fmt.Println(π) 27 | } 28 | -------------------------------------------------------------------------------- /multi_assign.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | a, b := 1, 2 25 | b, c := 3, 4 26 | fmt.Println(a, b, c) 27 | } 28 | -------------------------------------------------------------------------------- /strint_conv.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "strconv" 22 | ) 23 | 24 | func main() { 25 | i := 169 26 | s := strconv.Itoa(i) 27 | fmt.Println(s) 28 | } 29 | -------------------------------------------------------------------------------- /two_cities.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | city1, city2 := "Kraków", "Kraków" 25 | fmt.Println(city1 == city2) 26 | } 27 | -------------------------------------------------------------------------------- /append.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | a := []int{1, 2, 3} 25 | b := append(a[:1], 10) 26 | fmt.Printf("a=%v, b=%v\n", a, b) 27 | } 28 | -------------------------------------------------------------------------------- /cityu.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "unicode/utf8" 22 | ) 23 | 24 | func main() { 25 | city := "Kraków" 26 | fmt.Println(utf8.RuneCountInString(city)) 27 | } 28 | -------------------------------------------------------------------------------- /flag.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | type Flag int 24 | 25 | func main() { 26 | var i interface{} = 3 27 | f := i.(Flag) 28 | fmt.Println(f) 29 | } 30 | -------------------------------------------------------------------------------- /strint_byte.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | data := []byte{'1', '2', '9'} 25 | s := string(data) 26 | fmt.Println(s) // 129 27 | } 28 | -------------------------------------------------------------------------------- /iota.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | const ( 24 | Read = 1 << iota 25 | Write 26 | Execute 27 | ) 28 | 29 | func main() { 30 | fmt.Println(Execute) 31 | } 32 | -------------------------------------------------------------------------------- /init.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func init() { 24 | fmt.Printf("A ") 25 | } 26 | 27 | func init() { 28 | fmt.Print("B ") 29 | } 30 | 31 | func main() { 32 | fmt.Println() 33 | } 34 | -------------------------------------------------------------------------------- /next_id.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | var ( 24 | id = nextID() 25 | ) 26 | 27 | func nextID() int { 28 | id++ 29 | return id 30 | } 31 | 32 | func main() { 33 | fmt.Println(id) 34 | } 35 | -------------------------------------------------------------------------------- /time.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "time" 22 | ) 23 | 24 | func main() { 25 | timeout := 3 26 | fmt.Printf("before ") 27 | time.Sleep(timeout * time.Millisecond) 28 | fmt.Println("after") 29 | } 30 | -------------------------------------------------------------------------------- /chan.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | ch := make(chan int, 2) 25 | ch <- 1 26 | ch <- 2 27 | <-ch 28 | close(ch) 29 | a := <-ch 30 | b := <-ch 31 | fmt.Println(a, b) 32 | } 33 | -------------------------------------------------------------------------------- /runes.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | msg := "π = 3.14159265358..." 25 | fmt.Printf("%T ", msg[0]) 26 | for _, c := range msg { 27 | fmt.Printf("%T\n", c) 28 | break 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /struct_pt.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | // Point is a 2D point 24 | type Point struct { 25 | X int 26 | Y int 27 | } 28 | 29 | func main() { 30 | p := Point{1, 2} 31 | fmt.Printf("%v\n", p) // {1 2} 32 | } 33 | -------------------------------------------------------------------------------- /flag_ok.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | type Flag int 24 | 25 | func main() { 26 | var i interface{} = 3 27 | f, ok := i.(Flag) 28 | if !ok { 29 | fmt.Println("not a Flag") 30 | return 31 | } 32 | fmt.Println(f) 33 | } 34 | -------------------------------------------------------------------------------- /chan_empty.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | ch := make(chan int, 1) 25 | ch <- 1 26 | a, ok := <-ch 27 | fmt.Println(a, ok) // 1 true 28 | close(ch) 29 | b, ok := <-ch 30 | fmt.Println(b, ok) // 0 false 31 | } 32 | -------------------------------------------------------------------------------- /strint_map.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | m := map[string]int{ 25 | "hello": 3, 26 | } 27 | key := []byte{'h', 'e', 'l', 'l', 'o'} 28 | val := m[string(key)] // no memory allocation 29 | fmt.Println(val) // 3 30 | } 31 | -------------------------------------------------------------------------------- /two_cities_nfc.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | 22 | "golang.org/x/text/unicode/norm" 23 | ) 24 | 25 | func main() { 26 | city1, city2 := "Kraków", "Kraków" 27 | city1, city2 = norm.NFC.String(city1), norm.NFC.String(city2) 28 | fmt.Println(city1 == city2) 29 | } 30 | -------------------------------------------------------------------------------- /struct.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "time" 22 | ) 23 | 24 | // Log is a log message 25 | type Log struct { 26 | Message string 27 | time.Time 28 | } 29 | 30 | func main() { 31 | ts := time.Date(2009, 11, 10, 0, 0, 0, 0, time.UTC) 32 | log := Log{"Hello", ts} 33 | fmt.Printf("%v\n", log) 34 | } 35 | -------------------------------------------------------------------------------- /count.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "sync" 22 | ) 23 | 24 | func main() { 25 | var count int 26 | var wg sync.WaitGroup 27 | 28 | for i := 0; i < 1_000_000; i++ { 29 | wg.Add(1) 30 | go func() { 31 | defer wg.Done() 32 | count++ 33 | }() 34 | 35 | } 36 | wg.Wait() 37 | fmt.Println(count) 38 | } 39 | -------------------------------------------------------------------------------- /sleep_sort.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "sync" 22 | "time" 23 | ) 24 | 25 | func main() { 26 | var wg sync.WaitGroup 27 | for _, n := range []int{3, 1, 2} { 28 | wg.Add(1) 29 | go func() { 30 | defer wg.Done() 31 | time.Sleep(time.Duration(n) * time.Millisecond) 32 | fmt.Printf("%d ", n) 33 | }() 34 | } 35 | wg.Wait() 36 | fmt.Println() 37 | } 38 | -------------------------------------------------------------------------------- /time_eq.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "encoding/json" 21 | "fmt" 22 | "log" 23 | "time" 24 | ) 25 | 26 | func main() { 27 | t1 := time.Now() 28 | data, err := json.Marshal(t1) 29 | if err != nil { 30 | log.Fatal(err) 31 | } 32 | 33 | var t2 time.Time 34 | if err := json.Unmarshal(data, &t2); err != nil { 35 | log.Fatal(err) 36 | } 37 | fmt.Println(t1 == t2) 38 | } 39 | -------------------------------------------------------------------------------- /json_float.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "encoding/json" 21 | "fmt" 22 | "log" 23 | ) 24 | 25 | func main() { 26 | n1 := 1 27 | data, err := json.Marshal(n1) 28 | if err != nil { 29 | log.Fatal(err) 30 | } 31 | 32 | var n2 interface{} 33 | if err := json.Unmarshal(data, &n2); err != nil { 34 | log.Fatal(err) 35 | } 36 | 37 | fmt.Printf("n1 is %T, n2 is %T\n", n1, n2) 38 | } 39 | -------------------------------------------------------------------------------- /range.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func fibs(n int) chan int { 24 | ch := make(chan int) 25 | 26 | go func() { 27 | a, b := 1, 1 28 | for i := 0; i < n; i++ { 29 | ch <- a 30 | a, b = b, a+b 31 | } 32 | }() 33 | 34 | return ch 35 | } 36 | 37 | func main() { 38 | for i := range fibs(5) { 39 | fmt.Printf("%d ", i) 40 | } 41 | fmt.Println() 42 | } 43 | -------------------------------------------------------------------------------- /sleep_sort_param.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "sync" 22 | "time" 23 | ) 24 | 25 | func main() { 26 | var wg sync.WaitGroup 27 | for _, n := range []int{3, 1, 2} { 28 | wg.Add(1) 29 | go func(i int) { 30 | defer wg.Done() 31 | time.Sleep(time.Duration(n) * time.Millisecond) 32 | fmt.Printf("%d ", i) 33 | }(n) 34 | } 35 | wg.Wait() 36 | fmt.Println() 37 | } 38 | -------------------------------------------------------------------------------- /count_mu.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "sync" 22 | ) 23 | 24 | func main() { 25 | var count int 26 | var wg sync.WaitGroup 27 | var m sync.Mutex 28 | 29 | for i := 0; i < 1_000_000; i++ { 30 | wg.Add(1) 31 | go func() { 32 | m.Lock() 33 | defer m.Unlock() 34 | defer wg.Done() 35 | count++ 36 | }() 37 | 38 | } 39 | wg.Wait() 40 | fmt.Println(count) 41 | } 42 | -------------------------------------------------------------------------------- /sleep_sort_scope.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "sync" 22 | "time" 23 | ) 24 | 25 | func main() { 26 | var wg sync.WaitGroup 27 | for _, n := range []int{3, 1, 2} { 28 | n := n // <1> 29 | wg.Add(1) 30 | go func() { 31 | defer wg.Done() 32 | time.Sleep(time.Duration(n) * time.Millisecond) 33 | fmt.Printf("%d ", n) 34 | }() 35 | } 36 | wg.Wait() 37 | fmt.Println() 38 | } 39 | -------------------------------------------------------------------------------- /count_atomic.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | "sync" 22 | "sync/atomic" 23 | ) 24 | 25 | func main() { 26 | var count int64 // Atomic works with int64, not int 27 | var wg sync.WaitGroup 28 | 29 | for i := 0; i < 1_000_000; i++ { 30 | wg.Add(1) 31 | go func() { 32 | defer wg.Done() 33 | atomic.AddInt64(&count, 1) 34 | }() 35 | 36 | } 37 | wg.Wait() 38 | fmt.Println(count) 39 | } 40 | -------------------------------------------------------------------------------- /range_close.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func fibs(n int) chan int { 24 | ch := make(chan int) 25 | 26 | go func() { 27 | defer close(ch) // <1> 28 | a, b := 1, 1 29 | for i := 0; i < n; i++ { 30 | ch <- a 31 | a, b = b, a+b 32 | } 33 | }() 34 | 35 | return ch 36 | } 37 | 38 | func main() { 39 | for i := range fibs(5) { 40 | fmt.Printf("%d ", i) 41 | } 42 | fmt.Println() 43 | } 44 | -------------------------------------------------------------------------------- /runes_lit.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | r1 := '™' 25 | fmt.Println(r1) // 8482 26 | fmt.Printf("%c\n", r1) // ™ 27 | 28 | r2 := '\x61' // \x - 2 digits 29 | fmt.Printf("%c\n", r2) // a 30 | 31 | r3 := '\u2122' // \u - 4 digits (8482 in hex) 32 | fmt.Printf("%c\n", r3) // ™ 33 | 34 | r4 := '\U00002122' // \U - 8 digits 35 | fmt.Printf("%c\n", r4) // ™ 36 | } 37 | -------------------------------------------------------------------------------- /error.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | type OSError int 24 | 25 | func (e *OSError) Error() string { 26 | return fmt.Sprintf("error #%d", *e) 27 | } 28 | 29 | func FileExists(path string) (bool, error) { 30 | var err *OSError 31 | return false, err // TODO 32 | } 33 | 34 | func main() { 35 | if _, err := FileExists("/no/such/file"); err != nil { 36 | fmt.Printf("error: %s\n", err) 37 | } else { 38 | fmt.Println("OK") 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /iota_check.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | const ( 24 | Read = 1 << iota 25 | Write 26 | Execute 27 | ) 28 | 29 | func main() { 30 | mask := Read | Execute 31 | 32 | if mask&Execute == 0 { 33 | fmt.Println("can't execute") 34 | } else { 35 | fmt.Println("can execute") // will be printed 36 | } 37 | 38 | if mask&Write == 0 { 39 | fmt.Println("can't write") // will be printed 40 | } else { 41 | fmt.Println("can write") 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /range_rcv.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func fibs(n int) chan int { 24 | ch := make(chan int) 25 | 26 | go func() { 27 | defer close(ch) 28 | a, b := 1, 1 29 | for i := 0; i < n; i++ { 30 | ch <- a 31 | a, b = b, a+b 32 | } 33 | }() 34 | 35 | return ch 36 | } 37 | 38 | func main() { 39 | ch := fibs(5) 40 | for { 41 | i, ok := <-ch 42 | if !ok { 43 | break 44 | } 45 | fmt.Printf("%d ", i) 46 | } 47 | fmt.Println() 48 | } 49 | -------------------------------------------------------------------------------- /job.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | type Job struct { 24 | State string 25 | done chan struct{} 26 | } 27 | 28 | func (j *Job) Wait() { 29 | <-j.done 30 | } 31 | 32 | func (j *Job) Done() { 33 | j.State = "done" 34 | close(j.done) 35 | } 36 | 37 | func main() { 38 | ch := make(chan Job) 39 | go func() { 40 | j := <-ch 41 | j.Done() 42 | }() 43 | 44 | job := Job{"ready", make(chan struct{})} 45 | ch <- job 46 | job.Wait() 47 | fmt.Println(job.State) 48 | } 49 | -------------------------------------------------------------------------------- /job_ptr.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | type Job struct { 24 | State string 25 | done chan struct{} 26 | } 27 | 28 | func (j *Job) Wait() { 29 | <-j.done 30 | } 31 | 32 | func (j *Job) Done() { 33 | j.State = "done" 34 | close(j.done) 35 | } 36 | 37 | func main() { 38 | ch := make(chan *Job) 39 | go func() { 40 | j := <-ch 41 | j.Done() 42 | }() 43 | 44 | job := Job{"ready", make(chan struct{})} 45 | ch <- &job 46 | job.Wait() 47 | fmt.Println(job.State) 48 | } 49 | -------------------------------------------------------------------------------- /append_impl.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | a := []int{1, 2, 3} 25 | a = Append(a, 4) 26 | fmt.Println(a) 27 | b := a[:3] 28 | b = append(b, 5) 29 | fmt.Println(b) 30 | } 31 | 32 | func Append(items []int, i int) []int { 33 | if len(items) == cap(items) { // No more space in underlying array 34 | // Go has a better growth heuristic than adding 1 every append 35 | newItems := make([]int, len(items)+1) 36 | copy(newItems, items) 37 | items = newItems 38 | } else { 39 | items = items[:len(items)+1] 40 | } 41 | 42 | items[len(items)-1] = i 43 | return items 44 | } 45 | -------------------------------------------------------------------------------- /iota_str.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | type FilePerm uint16 // 16 flags are enough 24 | 25 | const ( 26 | Read FilePerm = 1 << iota 27 | Write 28 | Execute 29 | ) 30 | 31 | // String implements fmt.Stringer interface 32 | func (p FilePerm) String() string { 33 | switch p { 34 | case Read: 35 | return "read" 36 | case Write: 37 | return "write" 38 | case Execute: 39 | return "execute" 40 | } 41 | 42 | return fmt.Sprintf("unknown FilePerm: %d", p) // don't use %s here :) 43 | } 44 | 45 | func main() { 46 | fmt.Println(Execute) // execute 47 | fmt.Printf("%d\n", Execute) // 4 48 | } 49 | -------------------------------------------------------------------------------- /range_ctx.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "context" 21 | "fmt" 22 | ) 23 | 24 | func fibs(ctx context.Context, n int) chan int { 25 | ch := make(chan int) 26 | go func() { 27 | defer close(ch) 28 | a, b := 1, 1 29 | for i := 0; i < n; i++ { 30 | select { 31 | case ch <- a: 32 | a, b = b, a+b 33 | case <-ctx.Done(): 34 | fmt.Println("cancelled") 35 | } 36 | } 37 | }() 38 | 39 | return ch 40 | } 41 | 42 | func main() { 43 | ctx, cancel := context.WithCancel(context.Background()) 44 | ch := fibs(ctx, 5) 45 | for i := 0; i < 3; i++ { 46 | val := <-ch 47 | fmt.Printf("%d ", val) 48 | } 49 | fmt.Println() 50 | cancel() 51 | } 52 | -------------------------------------------------------------------------------- /num_lit.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2020 Miki Tebeka 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package main 18 | 19 | import ( 20 | "fmt" 21 | ) 22 | 23 | func main() { 24 | // Integer 25 | printNum(10) // 10 of type int 26 | printNum(010) // 8 of type int 27 | printNum(0x10) // 16 of type int 28 | printNum(0b10) // 2 of type int 29 | printNum(1_000) // 1000 of type int <1> 30 | 31 | // Float 32 | printNum(3.14) // 3.14 of type float64 33 | printNum(.2) // 0.2 of type float64 34 | printNum(1e3) // 1000 of type float64 35 | printNum(0x1p-2) // 0.25 of type float64 <3> 36 | 37 | // Complex 38 | printNum(1i) // (0+1i) of type complex128 39 | printNum(3 + 7i) // (3+7i) of type complex128 40 | printNum(1 + 0i) // (1+0i) of type complex128 41 | } 42 | 43 | func printNum(n interface{}) { 44 | fmt.Printf("%v of type %T\n", n, n) 45 | } 46 | --------------------------------------------------------------------------------