-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
测试了go-redis 实现解析和转化方法,替代在拿数据的时候进行手动的解析 测试net包发送post请求功能
- Loading branch information
Showing
13 changed files
with
301 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/redis/go-redis/v9" | ||
"log" | ||
) | ||
|
||
type RedisObj struct { | ||
*redis.Client | ||
} | ||
|
||
func NewRedis() RedisObj { | ||
client := redis.NewClient(&redis.Options{ | ||
Addr: "peifeng.site:6379", // redis地址 | ||
Password: "ningzaichun", // 密码 | ||
DB: 0, // 使用默认数据库 | ||
}) | ||
return RedisObj{client} | ||
} | ||
|
||
type AutoGenerated struct { | ||
DpvMatchCode string `json:"dpv_match_code"` | ||
DpvFootnotes string `json:"dpv_footnotes"` | ||
DpvCmra string `json:"dpv_cmra"` | ||
DpvVacant string `json:"dpv_vacant"` | ||
Active string `json:"active"` | ||
} | ||
|
||
func (m *AutoGenerated) MarshalBinary() (data []byte, err error) { | ||
return json.Marshal(m) | ||
} | ||
|
||
func (m *AutoGenerated) UnmarshalBinary(data []byte) error { | ||
return json.Unmarshal(data, m) | ||
|
||
} | ||
|
||
func main() { | ||
r := NewRedis() | ||
ctx := context.Background() | ||
a := &AutoGenerated{ | ||
DpvMatchCode: "Y", | ||
DpvFootnotes: "AABB", | ||
DpvCmra: "N", | ||
DpvVacant: "N", | ||
Active: "Y", | ||
} | ||
|
||
result, err := r.RPush(ctx, "666", a).Result() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(result) | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# lpush | ||
|
||
这个功能用的时候要注意不能直接放结构体变量进去, | ||
一般来说还是自己做解析,(记得可以自己为其实现 解析和读取的方法,然后就可以放了) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type name struct { | ||
} | ||
|
||
func main() { | ||
var i interface{} | ||
// 检查 interface 变量是否为 nil | ||
if i == nil { | ||
fmt.Println("i 是 nil") | ||
} else { | ||
fmt.Println("i 不是 nil") | ||
} | ||
i = nil | ||
if i == nil { | ||
fmt.Println("i 是 nil") | ||
} else { | ||
fmt.Println("i 不是 nil") | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
type Corp struct { | ||
ID int32 `json:"id" gorm:"Column:id;type:int;not null;primaryKey:KEY"` | ||
Cid string `json:"cid,omitempty" gorm:"Column:cid;type:varchar(32);not null;default:'';uniqueIndex:KEY"` // Cid 集团用户注册登录时的用户名 | ||
|
||
} | ||
|
||
func main() { | ||
var p Corp | ||
marshal, err := json.Marshal(p) | ||
if err != nil { | ||
return | ||
} | ||
fmt.Println(string(marshal)) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
INSERT INTO address (uname, phone, address, u_id) VALUES | ||
('张三', '13812345678', '北京市朝阳区', 85), | ||
('李四', '13912345678', '上海市浦东新区', 85), | ||
('王五', '13612345678', '广州市天河区', 85), | ||
('赵六', '13712345678', '深圳市南山区', 85), | ||
('钱七', '13512345678', '成都市武侯区', 85), | ||
('孙八', '13412345678', '杭州市西湖区', 85), | ||
('周九', '13312345678', '南京市玄武区', 85), | ||
('吴十', '13212345678', '武汉市江岸区', 85), | ||
('郑十一', '13112345678', '重庆市渝中区', 85), | ||
('王十二', '13012345678', '长沙市岳麓区', 85), | ||
('冯十三', '13912345679', '西安市雁塔区', 85), | ||
('陈十四', '13912345670', '郑州市金水区', 85), | ||
('楚十五', '13912345671', '苏州市姑苏区', 85), | ||
('魏十六', '13912345672', '天津市和平区', 85), | ||
('蔡十七', '13912345673', '青岛市市北区', 85), | ||
('曹十八', '13912345674', '大连市西岗区', 85), | ||
('庞十九', '13912345675', '沈阳市和平区', 85), | ||
('严二十', '13912345676', '长春市南关区', 85), | ||
('丁二十一', '13912345677', '哈尔滨市道里区', 85), | ||
('贾二十二', '13912345680', '南昌市东湖区', 85); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package api | ||
|
||
var BaseUrl = `http://peifeng.site:8000/` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type Order struct { | ||
*http.Client | ||
} | ||
|
||
func (o *Order) OrderAdd(data map[string]any) { | ||
url := BaseUrl + "order/add" | ||
// 将数据编码为 JSON 格式 | ||
jsonData, err := json.Marshal(data) | ||
if err != nil { | ||
fmt.Println("Error encoding JSON:", err) | ||
return | ||
} | ||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) | ||
if err != nil { | ||
fmt.Println("Error creating request:", err) | ||
return | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
resp, err := o.Do(req) | ||
if err != nil { | ||
fmt.Println("Error sending request:", err) | ||
return | ||
} | ||
|
||
defer resp.Body.Close() | ||
// 打印响应内容 | ||
fmt.Println("Response:", resp.Status) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/peifengll/tests/testnet/req/api" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
client := &http.Client{} | ||
o := api.Order{client} | ||
redata := map[string]any{ | ||
"user_id": 85, | ||
"money": 78, | ||
"cart_id": "[43]", | ||
"address_id": 3, | ||
} | ||
o.OrderAdd(redata) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRandom(t *testing.T) { | ||
// 设置种子 | ||
rand.Seed(time.Now().UnixNano()) | ||
|
||
// 生成随机数 | ||
randomNum := rand.Intn(30) // 生成 0 到 100 之间的随机整数 | ||
fmt.Println("随机数:", randomNum) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
// 1、与服务端建立连接 | ||
conn, err := net.Dial("tcp", "127.0.0.1:9090") | ||
if err != nil { | ||
fmt.Printf("conn server failed, err:%v\n", err) | ||
return | ||
} | ||
// 2、使用 conn 连接进行数据的发送和接收 | ||
input := bufio.NewReader(os.Stdin) | ||
for { | ||
s, _ := input.ReadString('\n') | ||
s = strings.TrimSpace(s) | ||
if strings.ToUpper(s) == "Q" { | ||
return | ||
} | ||
|
||
_, err = conn.Write([]byte(s)) | ||
if err != nil { | ||
fmt.Printf("send failed, err:%v\n", err) | ||
return | ||
} | ||
// 从服务端接收回复消息 | ||
var buf [1024]byte | ||
n, err := conn.Read(buf[:]) | ||
if err != nil { | ||
fmt.Printf("read failed:%v\n", err) | ||
return | ||
} | ||
fmt.Printf("收到服务端回复:%v\n", string(buf[:n])) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
) | ||
|
||
func process(conn net.Conn) { | ||
// 处理完关闭连接 | ||
defer conn.Close() | ||
|
||
// 针对当前连接做发送和接受操作 | ||
for { | ||
reader := bufio.NewReader(conn) | ||
var buf [128]byte | ||
n, err := reader.Read(buf[:]) | ||
if err != nil { | ||
fmt.Printf("read from conn failed, err:%v\n", err) | ||
break | ||
} | ||
|
||
recv := string(buf[:n]) | ||
fmt.Printf("收到的数据:%v\n", recv) | ||
|
||
// 将接受到的数据返回给客户端 | ||
_, err = conn.Write([]byte("ok")) | ||
if err != nil { | ||
fmt.Printf("write from conn failed, err:%v\n", err) | ||
break | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
// 建立 tcp 服务 | ||
listen, err := net.Listen("tcp", "127.0.0.1:9090") | ||
if err != nil { | ||
fmt.Printf("listen failed, err:%v\n", err) | ||
return | ||
} | ||
|
||
for { | ||
// 等待客户端建立连接 | ||
conn, err := listen.Accept() | ||
if err != nil { | ||
fmt.Printf("accept failed, err:%v\n", err) | ||
continue | ||
} | ||
// 启动一个单独的 goroutine 去处理连接 | ||
go process(conn) | ||
} | ||
} |