Last active
March 27, 2023 07:03
-
-
Save hungle00/fd5bd1ec2ba19990d2b84b4b16c9bb23 to your computer and use it in GitHub Desktop.
Some go snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "golang.org/x/tour/tree" | |
| "fmt" | |
| ) | |
| // Walk walks the tree t sending all values | |
| // from the tree to the channel ch. | |
| func Walk(t *tree.Tree, ch chan int) { | |
| if(t == nil) { return } | |
| Walk(t.Left, ch) | |
| ch <- t.Value | |
| fmt.Println(t.Value) | |
| Walk(t.Right, ch) | |
| } | |
| // Same determines whether the trees | |
| // t1 and t2 contain the same values. | |
| func Same(t1, t2 *tree.Tree) bool { | |
| ch1 := make(chan int) | |
| ch2 := make(chan int) | |
| go Walk(t1, ch1) | |
| go Walk(t2, ch2) | |
| fmt.Println(cap(ch1)) | |
| fmt.Println(cap(ch2)) | |
| for i := 0; i < cap(ch2); i++ { | |
| j := <- ch1 | |
| fmt.Println(i, j) | |
| if(i != j) { return false } | |
| } | |
| return true; | |
| } | |
| func main() { | |
| ch := make(chan int) | |
| Walk(tree.New(1), ch) | |
| fmt.Println("result") | |
| fmt.Println(Same(tree.New(1), tree.New(1))) | |
| fmt.Println(Same(tree.New(1), tree.New(2))) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "golang.org/x/tour/pic" | |
| // func Test(dx, dy int) [][]uint8 { | |
| // arr := make([][]uint8, dy) | |
| // for i := range arr { | |
| // arr[i] = make([]uint8, dx) | |
| // } | |
| // return arr | |
| // } | |
| func Pic(dx, dy int) [][]uint8 { | |
| arr := make([][]uint8, dy) | |
| for i := range arr { | |
| arr[i] = make([]uint8, dx) | |
| for j := range arr[i] { | |
| arr[i][j] = uint8(i + j) | |
| } | |
| } | |
| return arr | |
| } | |
| func main() { | |
| pic.Show(Pic) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| func unique(nums []int) []int { | |
| map_result := make(map[int]bool) | |
| var result []int | |
| for _, num := range nums { | |
| _, ok := map_result[num] | |
| if !ok { | |
| map_result[num] = false | |
| result = append(result, num) | |
| } | |
| } | |
| return result | |
| } | |
| func main() { | |
| nums := []int{1, 2, 2, 3, 5, 6, 5} | |
| fmt.Println(unique(nums)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "strings" | |
| "golang.org/x/tour/wc" | |
| ) | |
| func WordCount(s string) map[string]int { | |
| word_count := make(map[string]int) | |
| arr_words := strings.Fields(s) | |
| for _, word := range arr_words { | |
| _, ok := word_count[word] | |
| if ok { | |
| word_count[word] += 1 | |
| } else { | |
| word_count[word] = 1 | |
| } | |
| } | |
| return word_count | |
| } | |
| func main() { | |
| wc.Test(WordCount) | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://zetcode.com/golang/filter-map/