Skip to content

Commit

Permalink
tip(go/algorithm): up
Browse files Browse the repository at this point in the history
  • Loading branch information
TIGERB committed Dec 20, 2020
1 parent 2630928 commit 3e420c9
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 206 deletions.
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<a href="https://github.com/TIGERB/easy-tips#目录">
<img src="https://img.shields.io/badge/PHP-✅-brightgreen.svg" alt="php">
</a>
<a href="https://github.com/TIGERB/easy-tips/tree/master/mysql">
<img src="https://img.shields.io/badge/Go-🚗-blue.svg" alt="mysql">
<a href="https://github.com/TIGERB/easy-tips/tree/master/go">
<img src="https://img.shields.io/badge/Go-🚗-blue.svg" alt="go">
</a>
<a href="https://github.com/TIGERB/easy-tips/tree/master/mysql">
<img src="https://img.shields.io/badge/MySQL-🚗-blue.svg" alt="mysql">
Expand Down Expand Up @@ -66,14 +66,16 @@
* [文档](https://github.com/TIGERB/easy-tips/blob/master/php/standard.md)
* [经验](https://github.com/TIGERB/easy-tips/blob/master/php/artisan.md)
+ [记一些PHP的坑](https://github.com/TIGERB/easy-tips/blob/master/pit.md#记一些坑)
- Go基础学习 🚗
- Go语言学习 🚗
+ Go框架源码阅读&解析
* [Go框架解析-beego](http://tigerb.cn/2018/12/06/beego/)
* [Go框架解析-iris](http://tigerb.cn/2019/06/29/go-iris/)
* [Go框架解析-gin](http://tigerb.cn/2019/07/06/go-gin/)
* [Go框架解析-echo](http://tigerb.cn/2019/07/13/go-echo/)
+ go常用包解析
+ Go常用包解析
* go常用包解析-fasthttp
+ Go语言进阶学习
* [由浅到深,入门Go语言Map实现原理](http://tigerb.cn/2020/12/20/go-base/map/)
- 高并发相关 🚗
+ [处理高并发的一般思路](http://tigerb.cn/2019/04/18/top-qps-experience/)
+ [秒杀系统设计](http://tigerb.cn/2020/05/05/skrshop/seckill/)
Expand Down Expand Up @@ -136,13 +138,14 @@
+ [概念](https://github.com/TIGERB/easy-tips/blob/master/patterns/thought.md#设计模式)
+ [面向对象的设计过程](http://tigerb.cn/2019/10/11/oop/)
+ Go版本 🚗
* [我的代码没有else系列](https://github.com/TIGERB/easy-tips/tree/master/go/src/patterns)
- [模板模式](https://github.com/TIGERB/easy-tips/tree/master/go/src/patterns/template)
- [责任链模式](https://github.com/TIGERB/easy-tips/tree/master/go/src/patterns/responsibility)
- [组合模式](https://github.com/TIGERB/easy-tips/tree/master/go/src/patterns/composite)
- [观察者模式](https://github.com/TIGERB/easy-tips/tree/master/go/src/patterns/observer)
- [策略模式](https://github.com/TIGERB/easy-tips/tree/master/go/src/patterns/strategy)
- [状态模式](https://github.com/TIGERB/easy-tips/tree/master/go/src/patterns/state)
* [我的代码没有else系列](https://github.com/TIGERB/easy-tips/tree/master/go/patterns)
- [模板模式](https://github.com/TIGERB/easy-tips/tree/master/go/patterns/template)
- [责任链模式](https://github.com/TIGERB/easy-tips/tree/master/go/patterns/responsibility)
- [组合模式](https://github.com/TIGERB/easy-tips/tree/master/go/patterns/composite)
- [观察者模式](https://github.com/TIGERB/easy-tips/tree/master/go/patterns/observer)
- [策略模式](https://github.com/TIGERB/easy-tips/tree/master/go/patterns/strategy)
- [状态模式](https://github.com/TIGERB/easy-tips/tree/master/go/patterns/state)
- [并发组合模式](https://github.com/TIGERB/easy-tips/blob/master/go/patterns/composite/README-Concurrency.md)
- ...
+ PHP版本 ✅
* 创建型模式实例
Expand Down
8 changes: 4 additions & 4 deletions go/algorithm/leetcode/144-tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func preorderTraversal(root *TreeNode) []int {
}
// 出栈
popStack := func(stack []*TreeNode) ([]*TreeNode, *TreeNode) {
len := len(stack)
if len == 0 {
lenStack := len(stack)
if lenStack == 0 {
return []*TreeNode{}, nil
}
val := stack[len-1]
return append(stack[:0], stack[:len-1]...), val
val := stack[lenStack-1]
return append(stack[:0], stack[:lenStack-1]...), val
}

// 根节点入栈
Expand Down
45 changes: 43 additions & 2 deletions go/algorithm/tree.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package algorithm

import "fmt"
import (
"fmt"
"math"
)

// Tree 二叉树
type Tree struct {
Expand Down Expand Up @@ -188,6 +191,44 @@ type TreeNode struct {
* }
*/
func isBalanced(root *TreeNode) bool {
stack := []*TreeNode{}
pushStack := func(stack []*TreeNode, val *TreeNode) []*TreeNode {
return append(stack, val)
}
popStack := func(stack []*TreeNode) ([]*TreeNode, *TreeNode) {
lenStack := len(stack)
if lenStack == 0 {
return []*TreeNode{}, nil
}
val := stack[lenStack-1]
return append(stack[:0], stack[:lenStack-1]...), val
}

// push根节点
stack = pushStack(stack, root)
node := &TreeNode{}
// 右节点深度
i := 0
// 左节点深度
j := 0
for len(stack) > 0 {
// 弹出
stack, node = popStack(stack)
fmt.Println(node.Val)
if node.Right != nil {
// 压栈
i++
stack = pushStack(stack, node.Right)
}
if node.Left != nil {
// 压栈
j++
stack = pushStack(stack, node.Left)
}
if math.Abs(float64(i-j)) > 1 {
return false
}
}

return false
return true
}
3 changes: 2 additions & 1 deletion go/go-learn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,5 @@ fmt.Println("d.PropertyTwo", d.PropertyTwo)

```
buckets
```
```

Binary file added go/go-learn/main
Binary file not shown.
173 changes: 4 additions & 169 deletions go/go-learn/main.go
Original file line number Diff line number Diff line change
@@ -1,173 +1,8 @@
package main

// GODEBUG=scheddetail=1,schedtrace=1000 ./go-learn
//
//
//
//
//
//
//
//
//
//

// type Base struct {
// }

// func (b *Base) Do() {
// fmt.Println("base")
// b.DoA()
// }

// func (b *Base) DoA() {
// fmt.Println("base DoA")
// }

// type Demo struct {
// Base
// }

// func (b *Demo) DoA() {
// fmt.Println("Demo DoA")
// }

func main() {
// (&Demo{}).Do()
// ctx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
// defer cancelFunc()
// logicResChan := make(chan bool, 1)
// go func(logicResChan chan bool) {
// fmt.Println("获取地址信息 ing1...")
// // bc.AddressInfo = &AddressInfo{
// // AddressID: 9931831,
// // FristName: "hei",
// // SecondName: "heihei",
// // }
// fmt.Println("获取地址信息 ing2...")
// time.Sleep(1 * time.Second)
// logicResChan <- true
// fmt.Println("获取地址信息 done...")
// }(logicResChan)

// fmt.Println("获取地址信息 wait...")

// select {
// case <-logicResChan:
// // 业务执行结果
// fmt.Println("获取地址信息 wait.done...")
// break
// case <-ctx.Done():
// // 超时退出
// fmt.Println("获取地址信息 timeout...")
// break
// }

// wg := &sync.WaitGroup{}

// wg.Add(1)
// go func(wg *sync.WaitGroup) {
// fmt.Println("aaa")
// wg.Done()
// }(wg)

// wg.Add(1)
// go func(wg *sync.WaitGroup) {
// fmt.Println("aaa")
// wg.Done()
// }(wg)

// wg.Add(1)
// go func(wg *sync.WaitGroup) {
// fmt.Println("aaa")
// wg.Done()
// }(wg)

// fmt.Println("bbb")

// wg.Wait()

// goroutine 顺序打印

// // context 控制多个 goroutine 超时
// ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
// defer cancelFunc()

// go func(ctx context.Context) {
// requestHandleResult := make(chan string, 1)
// go func(resChan chan<- string) {
// time.Sleep(3 * time.Second)
// resChan <- "success"
// }(requestHandleResult)
// select {
// case res := <-requestHandleResult:
// fmt.Println("requestHandleResult: " + res)
// case <-ctx.Done():
// fmt.Println("child | context.WithTimeout | ctx err: " + ctx.Err().Error())
// }
// }(ctx)

// select {
// case <-ctx.Done():
// fmt.Println("main | context.WithTimeout | ctx err: " + ctx.Err().Error())
// }

// runtime.GOMAXPROCS(runtime.NumCPU())
// debug.SetMaxThreads(runtime.NumCPU())
// go (func() {
// fmt.Println("aa")
// })()
// var a int32 = 1
// atomic.AddInt32(&a, 1)

// fmt.Println(a)

// fmt.Println("runtime.NumCPU()", runtime.NumCPU())
// runtime.GOMAXPROCS(runtime.NumCPU())

// wg := &sync.WaitGroup{}
// numbers := 10
// wg.Add(numbers)
// for range make([]byte, numbers) {
// go func(wg *sync.WaitGroup) {
// var counter int
// for i := 0; i < 1e10; i++ {
// counter++
// }
// wg.Done()
// }(wg)
// }

// wg.Wait()
// l, _ := net.Listen("tcp", ":9999")
// for {
// c, _ := l.Accept()
// if c == nil {
// continue
// }
// fmt.Println("c.RemoteAddr()", c.RemoteAddr())
// go func(c net.Conn) {
// defer c.Close()
// var body []byte
// for {
// _, err := c.Read(body)
// if err != nil {
// break
// }
// fmt.Println("body", string(body))
// c.Write(body)
// break
// }
// }(c)
// }

// // ------------------ 使用http包启动一个http服务 方式一 ------------------
// // *http.Request http请求内容实例的指针
// // http.ResponseWriter 写http响应内容的实例
// http.HandleFunc("/v1/demo", func(w http.ResponseWriter, r *http.Request) {
// // 写入响应内容
// w.Write([]byte("Hello TIGERB !\n"))
// })
// // 启动一个http服务并监听8888端口 这里第二个参数可以指定handler
// http.ListenAndServe(":8888", nil)
demo := map[string]string{}
demo["a"] = "aa"
demo["b"] = "bb"
demo["c"] = "cc"
}
43 changes: 24 additions & 19 deletions go/redis/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"strings"
"unsafe"
)

// 理解unsafe.Pointer笔记
Expand Down Expand Up @@ -46,27 +46,32 @@ func main() {

// ------------ 理解 uintptr

// d := &demo{
// PropertyOne: "one",
// PropertyTwo: "two",
// }
// fmt.Println("d", d)
// fmt.Printf("d %p", d)

// dStr := (*string)(unsafe.Pointer(d))
// fmt.Println("")
// fmt.Printf("dStr %p %v", dStr, *dStr)
// fmt.Println("")
// dTwoPointer := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(d)) + unsafe.Offsetof(d.PropertyTwo)))
// fmt.Println("uintptr", *(dTwoPointer))
// *dTwoPointer = "three"
// fmt.Println("uintptr", *(dTwoPointer))
// fmt.Println("d.PropertyTwo", d.PropertyTwo)
d := &demo{
PropertyOne: "one",
PropertyTwo: "two",
}
fmt.Println("d", d)
fmt.Printf("d %p", d)

dStr := (*string)(unsafe.Pointer(d))
fmt.Println("")
fmt.Printf("dStr %p %v", dStr, *dStr)
fmt.Println("")
dTwoPointer := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(d)) + unsafe.Offsetof(d.PropertyTwo)))
fmt.Println("uintptr", *(dTwoPointer))
*dTwoPointer = "three"
fmt.Println("uintptr", *(dTwoPointer))
fmt.Println("d.PropertyTwo", d.PropertyTwo)

var a float64 = 1.0
var b *int64
b = (*int64)(unsafe.Pointer(&a))
fmt.Println("a, b", &a, a, b, *b)

// ------------

a := "aaa"
// a := "aaa"

fmt.Println(strings.Count(a, ""), len(a))
// fmt.Println(strings.Count(a, ""), len(a))

}

0 comments on commit 3e420c9

Please sign in to comment.