60ke / old_blogs

https://60ke.github.io
0 stars 0 forks source link

时光机备忘 | LookForAdmin #2

Open 60ke opened 3 years ago

60ke commented 3 years ago

https://60ke.github.io/2019/05/31/%E6%97%B6%E5%85%89%E6%9C%BA%E5%A4%87%E5%BF%98/#more

学如逆水行舟,不进则退;心似平原跑马易放难收。

60ke commented 3 years ago

在Golang的goroutines完成之前阻止main()函数终止(Prevent the main() function from terminating before goroutines finish in Golang)

package main

import (
    "fmt"
    "sync"
)

var wg = &sync.WaitGroup{}

func printElo() {
    defer wg.Done()
    fmt.Printf("Elo\n")
}

func printHello() {
    defer wg.Done()
    fmt.Printf("Hello\n")
}

func main() {
    fmt.Printf("This will print.")
    i := 0
    for i < 10 {
        wg.Add(1)
        go printElo()
        wg.Add(1)
        go printHello()
        i++
    }
    wg.Wait()
}
60ke commented 3 years ago

vacode replace

([0-9a-f]+)->"$1"

before:

[[72756c652054784261736963 7265672074782e66756e6374696f6e3d3d2272656d6f76654174496e646578223a 2f2f2072656720313d3d31 2f2f20726571756972652074782e617267732e696e646578203c3d20353b 2f2f20726571756972652074782e617267732e5f7265636569766572735b305d203d3d2022307835393239656261333038353039383664653666393333393761383666396238303930313839366538223b 7265717569726520636f6e74726163742874782e746f292e73746174652e61727261795b305d203d3d20313b 7265717569726520636f6e74726163742874782e746f292e73746174652e61727261795b315d203d3d20323b 7265717569726520636f6e74726163742874782e746f292e73746174652e61727261795b325d203d3d20333b 656e64]]

after:

[["72756c652054784261736963" "7265672074782e66756e6374696f6e3d3d2272656d6f76654174496e646578223a" "2f2f2072656720313d3d31" "2f2f20726571756972652074782e617267732e696e646578203c3d20353b" "2f2f20726571756972652074782e617267732e5f7265636569766572735b305d203d3d2022307835393239656261333038353039383664653666393333393761383666396238303930313839366538223b" "7265717569726520636f6e74726163742874782e746f292e73746174652e61727261795b305d203d3d20313b" "7265717569726520636f6e74726163742874782e746f292e73746174652e61727261795b315d203d3d20323b" "7265717569726520636f6e74726163742874782e746f292e73746174652e61727261795b325d203d3d20333b" "656e64"]]
60ke commented 3 years ago

antlr4 -Dlanguage=Python3 RegLang.g4 -visitor -o dist

60ke commented 3 years ago
You can create and mount a ram based disk as follows:

Create a disk

hdiutil attach -nomount ram://$((2 * 1024 * SIZE_IN_MB))
hdiutil will return the name of the ramdisk.

Format and mount the disk

diskutil eraseVolume HFS+ RAMDisk NAME_OF_DISK
Access the disk under /Volumes/<diskname>

cd /Volumes/RAMDisk && touch testfile.txt && ls
Creating a 100MB ramdisk:

$ hdiutil attach -nomount ram://$((2 * 1024 * 100))
/dev/disk3

$ diskutil eraseVolume HFS+ RAMDisk /dev/disk3
Started erase on disk3
Unmounting disk
Erasing
Initialized /dev/rdisk3 as a 100 MB case-insensitive HFS Plus volume
Mounting disk
Finished erase on disk3 RAMDisk

umount -f /Volumes/RAMDisk
hdiutil detach /dev/disk4
60ke commented 3 years ago

golang decode json

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        var user struct {
            Name string `json:"name"`
        }

        decoder := json.NewDecoder(r.Body)
        decoder.DisallowUnknownFields()

        if err := decoder.Decode(&user); err != nil {
            http.Error(w, err.Error(), http.StatusBadRequest)
            return
        }

        fmt.Fprintf(w, "hi %s!", user.Name)
    })

    log.Fatal(http.ListenAndServe(":8080", nil))
}
60ke commented 3 years ago

opencore关机卡住问题: sudo kextcache -i /

60ke commented 3 years ago

vscode补全自定义

60ke commented 3 years ago

crtl+enter

60ke commented 3 years ago

How to wait for all goroutines to finish in Golang

package main

import (
        "fmt"
        "sync"
        "time"
)

func worker(wg *sync.WaitGroup, id int) {
    defer wg.Done()

    fmt.Printf("Worker %v: Started\n", id)
    time.Sleep(time.Second)
    fmt.Printf("Worker %v: Finished\n", id)
}

func main() {
    var wg sync.WaitGroup

    for i := 0; i < 5; i++ {
        fmt.Println("Main: Starting worker", i)
        wg.Add(1)
        go worker(&wg, i)
    }

    fmt.Println("Main: Waiting for workers to finish")
    wg.Wait()
    fmt.Println("Main: Completed")
}
60ke commented 3 years ago

gcc test.c -L/lib64 -ldtcsp -ldtrtl -lskf -Wl,-rpath /lib64/

60ke commented 3 years ago
gcc的其他常用的选项,

-c 编译成目标文件 如:gcc -c main.c 就是编译main.c成目标文件main.o

-I 头文件的查找路径,如:gcc -c main.c -I./inc 意思是:头文件的查找路径除了默认的之外,再加上./inc目录下的。

-L 库文件的查找路径,如:gcc -o main main.o -L./lib -ltest 说明:libtest.a 或者 libtest.so 库文件的查找路径除了默认之外,再加上./lib目录。
-MM 导出文件的依赖关系(用#include 中的内容)如:gcc -MM main.c找出main.c的所依赖的头文件

-o 生成最终目标

-D宏定义 相当于在C中些语句#define ... 如:-DPI=3.14 就相当于在文件里面写语句#define PI 3.14
动态库的搜索路径

dl对动态库的搜索路径如下(按顺序如下)

a.编译目标代码时指定的动态库搜索路径;(如果要指定程序行时在./lib目录下找库文件libtest.so,命令如下:gcc -o main main.c -L./lib -ltest -Wl,-rpath ./lib) ,其中,-Wl的意思是,后面的选项直接交给ld程序处理,-rpath选项是说更改搜索路径为后面的参数./lib

b.环境变量LD_LIBRARY_PATH指定的动态库搜索路径;

c.配置文件/etc/ld.so.conf中指定的动态库搜索路径;(修改完文件后,用ldconfig更新)

d.默认的动态库搜索路径/lib和/usr/lib;

8、一些常用的命令(与库有关的)

(1)、ld 是gcc的链接程序。

(2)、ldd是查看可执行文件中所依赖的库的程序,比如想查main程序用到了那些动态库,可以直接
ldd main

(3)、ldconfig用来更新文件/etc/ld.so.conf的修改生效。

(4)、nm用来查看.so库中的函数名字,标记是T的就是动态库里面生成的名字。如:nm /lib/libc*.so
60ke commented 2 years ago

golang中的sync.Mutex并不能限制对加锁变量的访问,如下代码:

package main

import (
    "fmt"
    "sync"
    "time"
)

type A struct {
    num int
    sync.Mutex
}

func main() {
    tar := A{}
    tar.Lock()
    tar.num = 3
    go func() {
        // tar.Lock()
                tar.num = 4
        fmt.Println("go func")
    }()
    time.Sleep(2 * time.Second)
    fmt.Println("main func")
    tar.Unlock()

}

goroutine依然可以访问修改tar数据,所以lock unlock成对出现,由程序员通过lock标识来限制并发冲突。

60ke commented 2 years ago

docker login -u "myusername" -p "mypassword" docker.io

60ke commented 2 years ago

convert []byte in Go to *char in c in CGO

bt := make([]byte, 10)
C _ char: = (* c.char) (unsafe.pointer (&bt))//false
C _ char: = (* c.char) (unsafe.pointer (&bt[0]))//true
60ke commented 2 years ago

linux查看各个目录说明: man hier

60ke commented 2 years ago

go中的for ch := range channel不运行close(channel)会造成阻塞。。。。

60ke commented 2 years ago

以太坊虽然不像https那样有数字证书,但是以太坊的地址是其对应公钥的后20字节带有"0x"的hash。

60ke commented 2 years ago

go test -run ^TestRegLang$ -bench=. -cpuprofile=cpu.prof

60ke commented 2 years ago

vscode cmd+f12可跳转到接口函数的具体实现。。。