Posts

Showing posts with the label Golang

Stop using third party packages that has many dependencies that the author didn't have a check before they use

33. "remove every backend project that uses node third party packages, they might has backdoor that could use your VPS as their VPS to act like a general web client for hackers"   it is even worse if you use npm in root VPS, because the npm will always use new version of a package or sub_package, how can you make sure your 3000 dependencies will not have a virus that could use your VPS root permission to do bad things?   it is the same for other package manager, for example, python pip. or golang package, java package, rust package, flutter package. in chinese, people usally call those machine that has those software running 'rou ji'. """ 利用 package manager 自动更新新版本的特性,做远控 轻而易举 就算不更新,利用现有的package,二进制文件或者脚本 一个post发到黑客服务器,它就知道你的电脑能不能被控制 对于root设备,被控制就是被完全控制,对于普通设备,就是被当成hacker web 客户端,可以被拿来在网上发垃圾信息、垃圾账号注册 到时候你的ip就会被当成坏ip被服务器墙掉 """

The golden principles for using Golang

1. return a clone or a copy of a variable between functions 2. use the memory pointer only after you specified parent-level(global) variables. Or simply remember the top-down variable lifetime structure

How to use V2ray to set up a Local DNS

Download and install v2ray cd ~/Downloads wget https://github.com/v2fly/v2ray-core/releases/download/v5. 1.0 /v2ray-linux-arm64-v8a.zip # uncompress the file, and use pwd to get the path export PATH= " $HOME /Downloads/v2ray: $PATH " source ~/.bashrc Create the v2ray config file //v2ray_config.json { "dns": { "hosts": { "domain:googleapis.cn": "googleapis.com", "domain:weloveparty.domain.local": "10.0.2.2" //"192.168.49.195" }, "servers": [ "8.8.8.8", "1.1.1.1", { "address": "223.5.5.5", "domains": [ "geosite:cn" ], "expectIPs": [ "geoip:cn" ], "port": 53 } ] }, "inbounds": [ { "listen": "127.0.0.1", "port": 10807, ...

How to use put files into Golang program

Here is how you use go built-in embed module:   cp -r ../client/dist ./dist import ( "io/fs" ) //go:embed dist/* var dist_storage embed.FS dist_storage, _ := fs.Sub(dist_storage, "dist" ) router.StaticFS( "/ui/" , http.FS(dist_storage)) ​

How to set mirrors for Golang (only necessary if you live in China, they have GFW that almost bans everything from outside

Add the following at the end of ~/.bashrc :  export GO111MODULE=on export GOPROXY=https://mirrors.aliyun.com/goproxy/ Then open a new terminal, run your golang code again. ​

Why rust is better than CPP and Golang?

1. Compare to CPP   According to my short experience of rust learning. Rust is more like CPP. Which basically has anything you need to create software. Even if it doesn’t, you could easily get it by installing dependencies. Rust has a better package manager and better syntax pre-check than CPP. 2. Compare to Golang Golang doesn’t have a good package manager. Golang has bad JSON support. https://docs.rs/json/latest/json https://gobyexample.com/json Golang is too simplified. Sometimes hard to do the engineering with a CPP mind. ​

gorm: how to deal with hasMany relationship

model (structure) definition  The column name needs to be capitalized here to let it work: type MessageRecord struct { // gorm.Model Userid string //Userid works, userid not work ChatID int64 //use Chat_id is also fine MessageID int } type Wallet struct { // gorm.Model Balance string LastUpdateTime int64 } type User struct { Userid string UserAddress string TaskFlag int Wallet Wallet `gorm:"embedded;embeddedPrefix:wallet_"` MessageRecords []MessageRecord `gorm:"ForeignKey:Userid;references:Userid"` } foreignKey : Specifies column name of the current model that is used as a foreign key to the join table references : Specifies column name of the reference’s table that is mapped to the foreign key of the join table initialization import ( "github.com/glebarez/sqlite" "gorm.io/gorm" ) db, err := gorm.Open(sqlite....

How to handle json map in Golang? How to handle interface type conversion?

func GetMapValueByKey(key string , mapData map [ string ] interface {}) interface {} { value, ok := mapData[key] if ok { return value } return nil } var transferList = mapTool.GetMapValueByKey( "token_transfers" , result) for _, transfer := range transferList.([] interface {}) { var block_timestamp = transfer.( map [ string ] interface {})[ "block_ts" ] var quantity = transfer.( map [ string ] interface {})[ "quant" ] var quantityDecimal = safemath.String_to_decimal(quantity.( string )).Div(safemath.String_to_decimal( "1000000000000000000" )) var quantityString = quantityDecimal.String() fmt.Println(block_timestamp, quantityString) } { " total ": 2 , " rangeTotal ": 2 , " contractInfo ": {} , " token_transfers ": [ { " transaction_id ": "1cf69dca430c8b8ae3c3c24aa2bf8707477" , " ...

How to use Golang with the help of Goland

1. I know you know how to use Go Modules https://yingshaoxo.blogspot.com/2020/11/how-to-get-started-to-use-go.html 2. But it’s not enough if you use Goland IDE  You’ll have to preferences -> go -> Go Modules ->Enable Go Modules 3. Now it’s time to rock with the knowledge of go testing and go packages https://yingshaoxo.blogspot.com/2021/04/golang-how-to-do-experiments-while.html https://yingshaoxo.blogspot.com/2021/06/how-to-create-package-with-golang.html 4. By the way, you may also be interested in how to handle extremely big or small numbers with golang So here you go: https://github.com/shopspring/decimal ​

How to use Golang to make a flutter plugin

1. compile golang codes into a aar file https://yingshaoxo.gitbook.io/go-tutorial-for-pythoner/go-for-android 2. create a flutter plugin https://flutter.dev/docs/development/packages-and-plugins/developing-packages#step-1-create-the-package-1 3. use the aar file https://stackoverflow.com/a/57075972/8667243 Congratulations  You have a common code base for windows, android, IOS now. More info: https://github.com/yingshaoxo/flutter-discoverpingableserviceonlocalnetwork ​

How to create a package with Golang

Init it go mod init github.com/yingshaoxo/my_package Create a sub module mkdir sub_module_folder cd sub_module_folder touch sub_module_file.go # sub_module_file. go package sub_module import "fmt" func Print_something() { fmt.Println( "yingshaoxo is something!" ) } Import and use it in the main package cd .. touch main.go # main. go package main import ( sub_module "github.com/yingshaoxo/my_package/sub_module_folder" ) func main() { sub_module.Print_something() } Run it  go run main.go More info and example https://github.com/yingshaoxo/minimum-golang-package-layout ​

Golang: How to do experiments with go tests in VSCode

Image
Why  I love python, to a large extend, is because I could quickly see the results from my operations. But for golang, it can be a little bit different, each time I write software, I have to compile it before I run it. It’s not a very good idea to put my experiment code in the main loop (or main function). How Then an idea came to my mind. Do you know tests? It’s used to test whether your function works as you expected. Why don’t we just use tests as our playground where we do all those crazy experiments? All we have to do is to use the print function inside of our test code: # main_test. go # make sure your file ends up with '_test.go' , and function starts with 'Test' , otherwise, it won 't work package main import ( "log" "testing" ) func TestGetExistsPort(t *testing.T) { ports := get_exists_port() log.Println(ports) if len(ports) == 0 { t.Fatalf("there should has ports") } } vscode has a...