共有 9 篇文章
📅 最近更新
2024-01-26
- 2024-09-18
RawMessage Scan&Value
json.RawMessage
以[]byte
形式存储json数据,但在父结构体marshal和unmarshal时不会重复序列化,仅仅将数据复制到新json字符串中
通过继承Scanner和Valuer实现结构体变量写入和读出数据库
2024-01-20
- 2024-09-18
接收端
首先我们有这样一段测试代码来接收 POST 请求,并返回其接收到的字段信息。
1package main
2
3import (
4 "fmt"
5 "log"
6 "net/http"
7)
8
9func urlencodedHandler(w http.ResponseWriter, r *http.Request) {
10 err := r.ParseForm()
11 if err != nil {
12 log.Printf("r.ParseForm(): %v", err)
13 return
14 }
15
16 result := ""
17 for k, v := range r.Form {
18 result += fmt.Sprintf("%s:%v\n", k, v)
19 }
20
21 fmt.Fprintf(w, result)
22}
23
24func multipartHandler(w http.ResponseWriter, r *http.Request) {
25 err := r.ParseMultipartForm(4 * 1024 * 1024)
26 if err != nil {
27 log.Printf("r.ParseForm(): %v", err)
28 return
29 }
30
31 result := ""
32 for k, v := range r.MultipartForm.Value {
33 result += fmt.Sprintf("%s:%v\n", k, v)
34 }
35
36 for k, v := range r.MultipartForm.File {
37 result += fmt.Sprintf("%s:%v\n", k, v)
38 }
39
40 fmt.Fprintf(w, result)
41}
42
43func main() {
44 http.HandleFunc("/urlencoded", urlencodedHandler)
45 http.HandleFunc("/multipart", multipartHandler)
46
47 log.Fatal(http.ListenAndServe(":8080", nil))
48}
发送 urlencoded 请求
urlencoded
主要用于纯文本请求,代码如下:
2024-01-06
- 2024-09-18
介绍
元素为[]byte
的队列的golang实现(适用于多线程环境下,当然单线程也能用
如果想更改队列的元素类型,请自行将queue [][]byte
中的[]byte
替换为其他类型,同时修改函数中的相关代码
2023-11-30
- 2024-09-18
若拉取时提示无法验证包,可以添加以下环境变量以信任此地址
关闭指定域名的包验证
1export GONOSUMDB=git.akvicor.com
关闭所有域名的包验证
1export GOSUMDB=off
2023-06-11
- 2024-09-18
将窗口标题中带有指定字符串的窗口置顶,使其显示在所有窗口之上
1package main
2
3import (
4 "golang.org/x/sys/windows"
5 "strings"
6 "syscall"
7 "unsafe"
8)
9
10func AlwaysOnTop() {
11 SetWindowAlwaysOnTop(GetWindowHandleByWindowName("Title"))
12}
13
14const SWP_NOSIZE = uintptr(0x0001)
15const SWP_NOMOVE = uintptr(0x0002)
16
17// This is dumb but Go doesn't like the inline conversion (see above image).
18func IntToUintptr(value int) uintptr {
19 return uintptr(value)
20}
21
22func SetWindowAlwaysOnTop(hwnd uintptr) {
23 user32dll := windows.MustLoadDLL("user32.dll")
24 setwindowpos := user32dll.MustFindProc("SetWindowPos")
25 setwindowpos.Call(hwnd, IntToUintptr(-1), 0, 0, 100, 100, SWP_NOSIZE|SWP_NOMOVE)
26}
27
28func GetWindowHandleByWindowName(window_name string) uintptr {
29 user32dll := windows.MustLoadDLL("user32.dll")
30 enumwindows := user32dll.MustFindProc("EnumWindows")
31
32 var the_handle uintptr
33 window_byte_name := []byte(window_name)
34
35 // Windows will loop over this function for each window.
36 wndenumproc_function := syscall.NewCallback(func(hwnd uintptr, lparam uintptr) uintptr {
37 // Allocate 100 characters so that it has something to write to.
38 var filename_data [100]uint16
39 max_chars := uintptr(100)
40
41 getwindowtextw := user32dll.MustFindProc("GetWindowTextW")
42 getwindowtextw.Call(hwnd, uintptr(unsafe.Pointer(&filename_data)), max_chars)
43
44 // If there's a match, save the value and return 0 to stop the iteration.
45 if strings.Contains(string(windows.UTF16ToString([]uint16(filename_data[:]))), string(window_byte_name)) {
46 the_handle = hwnd
47 return 0
48 }
49
50 return 1
51 })
52
53 // Call the above looping function.
54 enumwindows.Call(wndenumproc_function, uintptr(0))
55
56 return the_handle
57}
2023-06-11
- 2024-09-18
默认的go build -o xxx.exe
这样是没有图标的,不怎么好看。
首先下载文件:
1git clone https://github.com/akavel/rsrc.git
进入目录,把上面的代码编译一下
1go build rsrc.go # 然后有个rsrc.exe文件
就在rsrc的目录下创建个ico.manifest
,内容如下:
2023-02-10
- 2024-09-18
foreach变量
错误代码
1for _, v := range histories {
2 InsertByHistory(&v)
3}
正确代码
1for _, v := range histories {
2 h := v
3 InsertByHistory(&h)
4}
笔记
如以上代码,传递指针时必须声明一个新变量存储v
,否则会导致传递给函数的是histories
最后一个元素的首地址
2022-07-14
- 2024-09-18
选择要下载的版本
下载安装
1# 下载
2wget https://go.dev/dl/go1.18.4.linux-amd64.tar.gz
3# 解压
4mv go1.18.4.linux-amd64.tar.gz go.tar.gz
5tar -C /usr/local -xzf go.tar.gz
配置环境变量
1# 编辑.bashrc
2vim /root/.bashrc
3# 添加如下内容
4export GOPATH=/root/go
5export PATH=/usr/local/go/bin:$PATH:$GOPATH/bin
6# 设置代理
7# Set the GOPROXY environment variable
8export GOPROXY=https://goproxy.io,direct
9# Set environment variable allow bypassing the proxy for specified repos (optional)
10export GOPRIVATE=git.mycompany.com,github.com/my/private
使环境变量生效
1source /root/.bashrc
安装完成
查看安装的go版本