package config
import (
"context"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/go-redis/redis/v8" //"github.com/gomodule/redigo/redis"
_ "github.com/gofiber/fiber/v2"
"github.com/olivere/elastic/v7"
"github.com/programmerug/fibergorm/entities"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gorm.io/driver/mysql"
"gorm.io/gorm"
_ "gorm.io/hints"
)
var DB *gorm.DB
var ES *elastic.Client
var esUrl = "http://192.168.5.88:9205"
var RDB *redis.Client
var MONGO *mongo.Client
var OssCli *oss.Client
func Connect() (err error) {
//初始化数据库连接
dns := "root:123456@(localhost:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
DB, err = gorm.Open(mysql.Open(dns), &gorm.Config{})
if err != nil {
fmt.Printf("连接mysql出错,错误信息:%v", err)
} else {
fmt.Println("成功连接mysql!")
}
return
}
func ConnectEs() (err error) {
//连接客户端
ES, err := elastic.NewClient(elastic.SetURL(esUrl), elastic.SetSniff(false))
if err != nil {
// Handle error
panic(err)
}
// 获取版本号的直接API
esVersion, err := ES.ElasticsearchVersion(esUrl)
if err != nil {
panic(err)
fmt.Printf("连接es出错,错误信息:%v", err)
} else {
fmt.Println("成功连接es!")
fmt.Printf("es的版本为%s\n", esVersion)
}
return
}
func ConnectRedis() (err error) {
//Background返回一个非空的Context。 它永远不会被取消,没有值,也没有期限。
//它通常在main函数,初始化和测试时使用,并用作传入请求的顶级上下文。
var ctx = context.Background()
RDB := redis.NewClient(&redis.Options{
Addr: "192.168.5.86:8001",
Password: "12345678",
DB: 0,
})
_, err = RDB.Ping(ctx).Result()
if err != nil {
fmt.Printf("连接redis出错,错误信息:%v", err)
} else {
fmt.Println("成功连接redis!")
}
return
}
func ConnectMongoDB() (err error) {
// Set client options 建立mongodb连接
clientOptions := options.Client().ApplyURI("mongodb://127.0.0.1:27017/test")
// Connect to MongoDB
MONGO, err := mongo.Connect(context.TODO(), clientOptions)
if err != nil {
fmt.Printf("连接MongoDB出错,错误信息:%v", err)
}
err = MONGO.Ping(context.TODO(), nil)
if err != nil {
fmt.Printf("Ping MongoDB出错,错误信息:%v", err)
} else {
fmt.Println("成功连接MongoDB!")
}
return
}
//创建oss连接
func ConnectOss() (err error) {
var ossConfig entities.OssCon
OssCli, err = oss.New(ossConfig.OssEndpoint, ossConfig.OssAccessKeyID, ossConfig.OssAccessKeySecret)
if err != nil {
panic(err)
}else {
fmt.Println("成功连接Oss!")
}
return
}
package entities
import (
"fmt"
"log"
_ "time"
)
type Todo struct {
ID int `json:"id"`
Tiltle string `json:"title"`
Status bool `json:"status"`
}
type Language struct {
Content string `json:"content"`
}
type OssCon struct {
OssBucket string `json:"oss_bucket"`
OssEndpoint string `json:"oss_endpoint"`
// oss访问key
OssAccessKeyID string `json:"oss_access_key_id"`
// oss访问key secret
OssAccessKeySecret string `json:"oss_access_key_secret"`
}
type Info struct {
Name string
Price int `json:"price" from:"pri"`
}
type DBDrive struct {
Type string
Mysql Mysql
Badger Badger
}
type User struct {
Id int `PK`
Username string
Pwd string
}
type Mysql struct{}
type Badger struct{}
func (m Mysql) GetGlobalObj() {
fmt.Println("mysql func")
}
func (b Badger) GetGlobalObj() {
fmt.Println("badger func")
}
//xxl.Logger接口实现
type Logger struct{}
func (l *Logger) Info(format string, a ...interface{}) {
fmt.Println(fmt.Sprintf("自定义日志 - "+format, a...))
}
func (l *Logger) Error(format string, a ...interface{}) {
log.Println(fmt.Sprintf("自定义日志 - "+format, a...))
}
type Student struct {
Name string
Age int
}
package handlers
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/apache/rocketmq-client-go/v2"
"github.com/apache/rocketmq-client-go/v2/consumer"
"github.com/apache/rocketmq-client-go/v2/primitive"
_ "github.com/apache/rocketmq-client-go/v2/primitive"
"github.com/apache/rocketmq-client-go/v2/producer"
_ "github.com/apache/rocketmq-client-go/v2/producer"
"github.com/blugelabs/bluge"
dlp "github.com/bytedance/godlp"
"github.com/fatih/set"
_ "github.com/go-redis/redis/v8"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/google/go-querystring/query"
"github.com/kardianos/service"
_ "github.com/olivere/elastic/v7"
"github.com/pkg/errors"
"github.com/programmerug/fibergorm/config"
"github.com/programmerug/fibergorm/entities"
"github.com/xxl-job/xxl-job-executor-go"
"github.com/xxl-job/xxl-job-executor-go/example/task"
"go.mongodb.org/mongo-driver/bson"
"log"
"os"
"reflect"
"runtime"
"strconv"
"testing"
"time"
)
func GetDogs(c *fiber.Ctx) error {
var dogs []entities.Todo
config.DB.Find(&dogs)
return c.Status(200).JSON(dogs)
}
func GetDog(c *fiber.Ctx) error {
id := c.Params("id")
results := utils.ImmutableString(c.Params("id"))
log.Println("打印格式化参数:" + results)
//拷贝相关参数
// Make a copy
buffer := make([]byte, len(results))
copy(buffer, results)
resultCopy := string(buffer)
log.Println("打印拷贝参数:" + resultCopy)
var dog entities.Todo
result := config.DB.Find(&dog, id)
if result.RowsAffected == 0 {
return c.SendStatus(404)
}
return c.Status(200).JSON(&dog)
}
func Get(c *fiber.Ctx) error {
return c.Send([]byte("Hello, World!"))
}
func AddDog(c *fiber.Ctx) error {
dog := new(entities.Todo)
if err := c.BodyParser(dog); err != nil {
return c.Status(503).SendString(err.Error())
}
config.DB.Create(&dog)
return c.Status(201).JSON(dog)
}
func UpdateDog(c *fiber.Ctx) error {
dog := new(entities.Todo)
id := c.Params("id")
if err := c.BodyParser(dog); err != nil {
return c.Status(503).SendString(err.Error())
}
config.DB.Where("id = ?", id).Updates(&dog)
return c.Status(200).JSON(dog)
}
func RemoveDog(c *fiber.Ctx) error {
id := c.Params("id")
var dog entities.Todo
result := config.DB.Delete(&dog, id)
if result.RowsAffected == 0 {
return c.SendStatus(404)
}
return c.SendStatus(200)
}
func Create(c *fiber.Ctx) error {
e1 := entities.Language{Content: "golang"}
_, err := config.ES.Index().
Index("es").
Id("1").
BodyJson(e1).
Do(context.Background())
if err != nil {
panic(err)
}
return c.SendStatus(200)
}
func GetEs(c *fiber.Ctx) error {
query, err := config.ES.Get().Index("es").Id("1").Do(context.Background())
if err != nil {
panic(err)
}
var result entities.Language
if query.Found {
err := json.Unmarshal(query.Source, &result)
if err != nil {
fmt.Println(err)
}
fmt.Println(result.Content)
}
return c.Status(200).JSON(result)
}
func DeleteEs(c *fiber.Ctx) error {
//Background返回一个非空的Context。 它永远不会被取消,没有值,也没有期限。
//它通常在main函数,初始化和测试时使用,并用作传入请求的顶级上下文。
var ctx = context.Background()
response, err := config.ES.Delete().Index("es").Id("1").Do(ctx)
if err != nil {
panic(err)
}
//打印删除返回数据
fmt.Println(response)
return c.SendStatus(200)
}
func DeleteRedis(c *fiber.Ctx) error {
//Background返回一个非空的Context。 它永远不会被取消,没有值,也没有期限。
//它通常在main函数,初始化和测试时使用,并用作传入请求的顶级上下文。
var ctx = context.Background()
n, err := config.RDB.Del(ctx, "key1", "key2").Result()
if err != nil {
panic(err)
}
fmt.Printf("成功删除了 %v 个\n", n)
return c.SendStatus(200)
}
func GetRedis(c *fiber.Ctx) error {
//Background返回一个非空的Context。 它永远不会被取消,没有值,也没有期限。
//它通常在main函数,初始化和测试时使用,并用作传入请求的顶级上下文。
var ctx = context.Background()
vType, err := config.RDB.Type(ctx, "key").Result()
if err != nil {
panic(err)
}
return c.Status(200).JSON(vType)
}
func SetRedis(c *fiber.Ctx) error {
//Background返回一个非空的Context。 它永远不会被取消,没有值,也没有期限。
//它通常在main函数,初始化和测试时使用,并用作传入请求的顶级上下文。
var ctx = context.Background()
err := config.RDB.SetEX(ctx, "key", "value", time.Hour*2).Err()
if err != nil {
panic(err)
}
return c.SendStatus(200)
}
func GetMongoDB(c *fiber.Ctx) error {
// 2, 选择数据库ichunt 3, 选择表cron_log
connect := config.MONGO.Database("ichunt").Collection("cron_log")
var result struct {
Value float64
}
filter := bson.D{{"name", "pi"}}
err := connect.FindOne(context.TODO(), filter).Decode(result)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found a single document: %+v\n", result)
return c.Status(200).JSON(result)
}
func Consumer(c *fiber.Ctx) error {
// 设置推送消费者
pull, _ := rocketmq.NewPushConsumer(
//消费组
consumer.WithGroupName("testGroup"),
// namesrv地址
consumer.WithNameServer([]string{"127.0.0.1:9876"}),
)
// 必须先在 开始前
err := pull.Subscribe("Topic-test", consumer.MessageSelector{}, func(ctx context.Context, ext ...*primitive.MessageExt) (consumer.ConsumeResult, error) {
for i := range ext {
fmt.Printf("subscribe callback:%v \n", ext[i])
}
return consumer.ConsumeSuccess, nil
})
if err != nil {
fmt.Println(err.Error())
}
err = pull.Start()
if err != nil {
fmt.Println(err.Error())
os.Exit(-1)
}
time.Sleep(time.Hour)
err = pull.Shutdown()
if err != nil {
fmt.Printf("shutdown Consumer error:%s",err.Error())
}
return c.SendStatus(200)
}
func Producer(c *fiber.Ctx) error {
push, _ := rocketmq.NewProducer(
// 设置 nameSrvAddr
// nameSrvAddr 是 Topic 路由注册中心
producer.WithNameServer([]string{"127.0.0.1:9876"}),
// 指定发送失败时的重试时间
producer.WithRetry(2),
// 设置 Group
producer.WithGroupName("testGroup"),
)
// 开始连接
err := push.Start()
if err != nil {
fmt.Printf("start producer error: %s", err.Error())
os.Exit(1)
}
// 设置节点名称
topic := "Topic-test"
// 循坏发送信息 (同步发送)
for i := 0; i < 10; i++ {
msg := &primitive.Message{
Topic: topic,
Body: []byte("Hello RocketMQ Go Client" + strconv.Itoa(i)),
}
// 发送信息
res, err := push.SendSync(context.Background(),msg)
if err != nil {
fmt.Printf("send message error:%s\n",err)
}else {
fmt.Printf("send message success: result=%s\n",res.String())
}
}
// 关闭生产者
err = push.Shutdown()
if err != nil {
fmt.Printf("shutdown producer error:%s",err.Error())
}
return c.SendStatus(200)
}
// Bucket: 获取bucket存储空间
func Bucket() *oss.Bucket {
var ossConfig entities.OssCon
bucket, err := config.OssCli.Bucket(ossConfig.OssBucket)
if err != nil {
log.Println(err.Error())
return nil
}
return bucket
}
func Upload(c *fiber.Ctx) error{
filename := "hes.text"
ossPath := "oss/" + filename // oss不能以/开头
file, _ := os.Open(filename)
err := Bucket().PutObject(ossPath, file)
if err != nil {
fmt.Printf("上传oss失败: %+v\n", err.Error())
return c.SendStatus(503)
}
return c.SendStatus(200)
}
// 临时下载授权
func DownUrl(c *fiber.Ctx) error{
filepath := "oss/hes.text"
url, err := Bucket().SignURL(filepath, oss.HTTPGet, 3600)
if err != nil {
fmt.Printf("下载oss失败: %+v\n", err.Error())
return c.SendStatus(503)
}
return c.Status(200).JSON(url)
}
//运行相关参数
func GetTest(c *fiber.Ctx) error{
var name = flag.String("name", "everyon", "object")
names := "robot"
fmt.Printf("---:%s\n", names)
fmt.Printf("-----:%s\n", *name)
//var container = []string{"zero", "one", "two"}
container := map[int]string{0: "zero", 1: "one", 2: "two"}
fmt.Printf("The element is %q.\n", container[1])
block := "function"
{
block := "inner"
fmt.Printf("The block is %s.\n", block)
}
fmt.Printf("The block is %s.\n", block)
s1 := make([]int, 5)
fmt.Printf("The length of s1: %d\n", len(s1))
fmt.Printf("The capacity of s1: %d\n", cap(s1))
fmt.Printf("The value of s1: %d\n", s1)
s2 := make([]int, 5, 8)
fmt.Printf("The length of s2: %d\n", len(s2))
fmt.Printf("The capacity of s2: %d\n", cap(s2))
fmt.Printf("The value of s2: %d\n", s2)
return c.SendStatus(200)
}
func StaticHtml(c *fiber.Ctx) error {
if c.Path() == "/" || c.Path() == "" || c.Path() == "/index.html" {
ht := `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>go api doc</title>
<body>
<h3>hhh</h3>
<hr></hr>
<h1>卧槽 哈哈哈哈哈</h1>
</body>
</html>`
c.Response().Header.SetContentType("text/html; charset=utf-8") // 以HTML的方式解析显示
return c.Send([]byte(ht))
} else{
return c.Send([]byte("404"))
}
}
func SetHeader() map[string]string {
headers := make(map[string]string)
headers["X-Auth-AppId"] = "Auth.AppID"
headers["X-Auth-Token"] = "common.GlobalObjs.User.Token"
headers["X-Client-ID"] = "common.GlobalVar.ID"
return headers
}
//将对象解析成sql
func reflectSql(data interface{}) (error, string) {
v := reflect.ValueOf(data)
t := reflect.TypeOf(data)
if t.Kind() != reflect.Struct {
return errors.New("unsupported argument type"), ""
}
sql := "insert into " + t.Name()
sqlEle := " ("
sqlValue := " values("
fieldNum := t.NumField()
for i := 0; i < fieldNum; i++ {
switch t.Field(i).Type.Kind() {
case reflect.Int:
if i == fieldNum-1 {
sqlEle += t.Field(i).Name
sqlValue += fmt.Sprintf("%d", v.Field(i).Int())
} else {
sqlEle += t.Field(i).Name + ","
sqlValue += fmt.Sprintf("%d,", v.Field(i).Int())
}
case reflect.String:
if i == fieldNum-1 {
sqlEle += t.Field(i).Name
sqlValue += v.Field(i).String()
} else {
sqlEle += t.Field(i).Name + ","
sqlValue += v.Field(i).String() + ","
}
}
}
sqlEle += ")"
sqlValue += ")"
sql += sqlEle + sqlValue
return nil, sql
}
func SaveSql(c *fiber.Ctx) error{
info := entities.Info{
Name: "name1",
Price: 1,
}
err, sql := reflectSql(info)
fmt.Println(err, sql) // <nil> insert into Info (Name,Price) values(name1,1)
v, err := query.Values(info) // 会自动a-z排序、转义
fmt.Println(v.Encode(), err) // age=2&comments=%E6%B5%8B%E8%AF%95&name=aq <nil>
section1 := []string{"33", "22", "11"}
section2 := []string{"33", "22","66"}
hh := DifferenceString(section1, section2)
fmt.Println(hh) // [11]
return c.SendStatus(200)
}
// DifferenceString 取字符串切片的差集 (section1存在,不存在section2)
func DifferenceString(section1, section2 []string) (intersection []string) {
if len(section1) == 0 || len(section2) == 0 {
return section1
}
a := set.New(set.ThreadSafe)
b := set.New(set.ThreadSafe)
for _, v := range section1 {
a.Add(v)
}
for _, v := range section2 {
b.Add(v)
}
intersectionSet := set.Difference(a, b)
return set.StringSlice(intersectionSet)
}
// 取phone交集
func intersectionPhone(section1, section2 []string) (intersection []string) {
if len(section1) == 0 || len(section2) == 0 {
return nil
}
a := set.New(set.ThreadSafe)
b := set.New(set.ThreadSafe)
for _, v := range section1 {
a.Add(v)
}
for _, v := range section2 {
b.Add(v)
}
intersectionSet := set.Intersection(a, b)
return set.StringSlice(intersectionSet)
}
//超时控制
func Close() {
ch := make(chan struct{})
go func() {
// TODO s.Close()
time.Sleep(time.Second*2)
close(ch)
}()
select {
case <-ch:
fmt.Println("close ok")
case <-time.After(time.Second):
fmt.Println("close timeout")
}
}
//获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
func GetFirstDateOfMonth(d time.Time) time.Time {
d = d.AddDate(0, 0, -d.Day()+1)
return GetZeroTime(d)
}
//获取传入的时间所在月份的最后一天,即某月最后一天的23:59:59。如传入time.Now(), 返回当前月份的最后一天0点时间。
func GetLastDateOfMonth(d time.Time) time.Time {
d = d.AddDate(0, 0, -d.Day()+1)
d = Get23Time(d)
return d.AddDate(0, 1, -1)
}
//获取某一天的0点时间
func GetZeroTime(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
}
//获取某一天的0点时间
func Get23Time(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 23, 59, 59, 0, d.Location())
}
func Reflect() {
hh := entities.DBDrive{
Type: "Badger",
}
checkReflect(hh, hh.Type, "GetGlobalObj")
}
// inter一个嵌套结构体
func checkReflect(inter interface{}, attributeName, method string) {
v := reflect.ValueOf(inter)
dbObj := v.FieldByName(attributeName) // 根据字段名获取属性 // 获得struct inter内嵌套的struct attributeName
m := dbObj.MethodByName(method) // 获取 struct attributeName的method方法
m.Call([]reflect.Value{}) // 调用struct attributeName的method方法 m.Call([]reflect.Value{reflect.ValueOf("参数值")})
}
type Program struct{}
func (p *Program) Start(s service.Service) error {
fmt.Println("server start")
go p.run()
return nil
}
func (p *Program) run() {
fmt.Println("开机自启动服务 - run")
}
func (p *Program) Stop(s service.Service) error {
fmt.Println("server stop")
return nil
}
func runTime() {
NCPU := runtime.NumCPU()
runtime.GOMAXPROCS(NCPU)
serConfig := &service.Config{
Name: "hhhh",
DisplayName: "test2",
Description: "test 开机自启动",
}
pro := &Program{}
s, err := service.New(pro, serConfig)
if err != nil {
fmt.Println(err, "service.New() err")
}
if len(os.Args) > 1 {
if os.Args[1] == "install" {
err = s.Install()
if err != nil {
fmt.Println("install err", err)
} else {
fmt.Println("install success")
}
return
}
if os.Args[1] == "remove" {
err = s.Uninstall()
if err != nil {
fmt.Println("Uninstall err", err)
} else {
fmt.Println("Uninstall success")
}
return
}
}
err = s.Run() // 运行服务
if err != nil {
fmt.Println("s.Run err", err)
}
}
//寻找最长不含有重复字符的子串
func lengthOf(s string) int {
lastOccurred := make(map[rune]int)
start := 0
maxLength := 0
for i, ch := range []rune(s) {
if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
start = lastI + 1
}
if i - start + 1 > maxLength {
maxLength = i - start + 1
}
lastOccurred[ch] = i
}
return maxLength
}
// GetWeekDay returns the week day name of a week day index.
func GetWeekDay(index int) string {
if index < 0 || index > 6 {
return "Unknown"
}
weekDays := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
return weekDays[index]
}
// table-driven 单测法,就是将流程沉淀为一个可复用的模板、并交由机器自动生成;
// 人类则只需要准备数据部分,将自己的多条不同的数据一行行填充到表里,交给流程模板
// 去构造子测试用例、查表、跑数据、比对结果,写单测这事就大功告成了。
func TestGetWeekDay(t *testing.T) {
// a subtest named "index=0"
t.Run("index=0", func(t *testing.T) {
index := 0
want := "Sunday"
if got := GetWeekDay(index); got != want {
t.Errorf("GetWeekDay() = %v, want %v", got, want)
}
})
// a subtest named "index=1"
t.Run("index=1", func(t *testing.T) {
index := 1
want := "Monday"
if got := GetWeekDay(index); got != want {
t.Errorf("GetWeekDay() = %v, want %v", got, want)
}
})
}
func TestGetWeekDay1(t *testing.T) {
type args struct {
index int
}
tests := []struct {
name string
args args
want string
}{
//todo: add test case
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetWeekDay(tt.args.index); got != tt.want {
t.Errorf("getweekday() = %v, want %v", got, tt.want)
}
})
}
}
// 创建索引
func writeIndex(indexPath string) {
config := bluge.DefaultConfig(indexPath)
writer, err := bluge.OpenWriter(config)
if err != nil {
log.Fatalf("error opening writer: %v", err)
}
defer writer.Close()
// 新建文档
doc := bluge.NewDocument("example").
AddField(bluge.NewTextField("name", "bluge")).AddField(bluge.NewDateTimeField("created_at", time.Now()))
err = writer.Update(doc.ID(), doc)
if err != nil {
log.Fatalf("error updating document: %v", err)
}
}
// 批量创建
func batch(indexPath string) {
writer, err := bluge.OpenWriter(bluge.DefaultConfig(indexPath))
batch := bluge.NewBatch()
for i := 0; i < 10; i++ {
doc := bluge.NewDocument(fmt.Sprintf("example_%d", i)).
AddField(bluge.NewTextField(fmt.Sprintf("field_%d", i), fmt.Sprintf("value_%d", i%2))).AddField(bluge.NewDateTimeField("created_at", time.Now()))
batch.Insert(doc)
}
err = writer.Batch(batch)
if err != nil {
log.Fatalf("error executing batch: %v", err)
}
batch.Reset()
}
// 查询
func search(indexPath string) {
config := bluge.DefaultConfig(indexPath)
reader, err := bluge.OpenReader(config)
if err != nil {
log.Fatalf("error getting index reader: %v", err)
}
defer reader.Close()
query := bluge.NewMatchQuery("value_1").SetField("field_1")
request := bluge.NewTopNSearch(10, query).
WithStandardAggregations()
documentMatchIterator, err := reader.Search(context.Background(), request)
if err != nil {
log.Fatalf("error executing search: %v", err)
}
match, err := documentMatchIterator.Next()
for err == nil && match != nil {
err = match.VisitStoredFields(func(field string, value []byte) bool {
fmt.Printf("match: %s:%s\n", field, string(value))
return true
})
if err != nil {
log.Fatalf("error loading stored fields: %v", err)
}
fmt.Println(match)
match, err = documentMatchIterator.Next()
}
if err != nil {
log.Fatalf("error iterator document matches: %v", err)
}
}
func DlpDemoCtx(c *fiber.Ctx) error{
dlpDemo()
return c.SendStatus(200)
}
// 敏感信息参数过滤
func dlpDemo() {
caller := "replace.your.caller"
if eng, err := dlp.NewEngine(caller); err == nil {
eng.ApplyConfigDefault()
fmt.Printf("DLP %s Demo:\n\n", eng.GetVersion())
inStr := `我的邮件是abcd@abcd.com,
18612341234是我的电话
你家住在哪里啊? 我家住在北京市海淀区北三环西路43号,
mac地址 06-06-06-aa-bb-cc
收件人:张真人 手机号码:13900000000`
if outStr, _, err := eng.Deidentify(inStr); err == nil {
fmt.Printf("\t1. Deidentify( inStr: %s )\n", inStr)
fmt.Printf("\toutStr: %s\n", outStr)
//eng.ShowResults(results)
fmt.Println()
}
eng.Close()
} else {
fmt.Println("[dlp] NewEngine error: ", err.Error())
}
}
func Reader(c *fiber.Ctx) error {
reader := bufio.NewReader(os.Stdin)
fmt.Println("请输入你想要输入的内容,按回车结束")
fmt.Print("-> ")
text, err := reader.ReadString('\n')
if err != nil{
panic(fmt.Errorf("发生致命错误: %w \n", err))
}
fmt.Println("刚接收到的内容是:",text)
unix := time.Now().Unix();
fmt.Println("time:", unix)
return c.Send([]byte(text))
}
func XXLJob(c *fiber.Ctx) error{
exec := xxl.NewExecutor(
xxl.ServerAddr("http://dev-a-b2b-xxl-job.vandream.com/xxl-job-admin"),
xxl.AccessToken(""), //请求令牌(默认为空)
xxl.ExecutorIp(""), //可自动获取
xxl.ExecutorPort("9156"), //默认9999(非必填)
xxl.RegistryKey("obmp-apm"), //执行器名称
xxl.SetLogger(&entities.Logger{}), //自定义日志
)
exec.Init()
//设置日志查看handler
exec.LogHandler(func(req *xxl.LogReq) *xxl.LogRes {
return &xxl.LogRes{Code: 200, Msg: "", Content: xxl.LogResContent{
FromLineNum: req.FromLineNum,
ToLineNum: 2,
LogContent: "SettlementCheckHandler",
IsEnd: true,
}}
})
//注册任务handler
exec.RegTask("Task_Test", task.Test)
defer exec.Run()
return c.Next()
}
// 注册
func SaveUser(c *fiber.Ctx) error {
user := new(entities.User)
if err := c.BodyParser(user); err != nil {
return c.Status(503).SendString(err.Error())
}
config.DB.Create(&user)
return c.Status(201).JSON(user)
}
//登录
func ValidateUser(c *fiber.Ctx) error {
username := c.Params("username")
pwd := c.Params("pwd")
var user entities.User
result := config.DB.Find(&user, username, pwd)
if result.RowsAffected == 0 {
return errors.New("用户名或密码错误!")
}
return c.Status(200).JSON(user)
}
package main
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
"github.com/programmerug/fibergorm/config"
_ "github.com/programmerug/fibergorm/config"
"github.com/programmerug/fibergorm/entities"
"github.com/programmerug/fibergorm/handlers"
"github.com/pyroscope-io/pyroscope/pkg/agent/profiler"
"io/ioutil"
"log"
"net/http"
"time"
)
func main() {
//fiberInit()
ginInit()
//监控 服务端: docker run -it -p 4040:4040 pyroscope/pyroscope:latest server
// go get -u github.com/pyroscope-io/pyroscope/pkg/agent/profiler
// Pyroscope 是一套开源的效能即时监控平台,简单的Server 及Agent 架构,让开发者可以轻松监控代码效能,不管你要找10 秒或几分钟内的效能数据,都可以快速的即时呈现,
//开发者也不用在意装了此监控会造成任何效能上的负担。
//Pyroscope 背后的储存采用 目前只有支援3 种语言(Python, Ruby 及Go) 未来会 假设您还没导入任何效能分析工具或平台,那Pyroscope 会是您最好的选择。
profiler.Start(profiler.Config{
ApplicationName: "simple.golang.app",
ServerAddress: "http://localhost:4040",
})
}
/**
* fiber框架
*/
func fiberInit(){
app := fiber.New()
config.Connect()
app.Static("/", "/public")
// => http://localhost:3000/js/script.js
// => http://localhost:3000/css/style.css
app.Static("/prefix", "/public")
// => http://localhost:3000/prefix/js/script.js
// => http://localhost:3000/prefix/css/style.css
app.Static("*", "/public/index.html")
// => http://localhost:3000/any/path/shows/index/html
app.Get("/dogs", handlers.GetDogs)
app.Get("/list", handlers.Get)
app.Get("/dogs/:id", handlers.GetDog)
app.Post("/dogs", handlers.AddDog)
app.Put("/dogs/:id", handlers.UpdateDog)
app.Delete("/dogs/:id", handlers.RemoveDog)
app.Get("/test", handlers.GetTest)
config.ConnectEs()
//app.Get("/es", handlers.Create)
app.Get("/es", handlers.GetEs)
app.Delete("/deletes", handlers.DeleteEs)
config.ConnectRedis()
app.Put("/set", handlers.SetRedis)
app.Get("/get", handlers.GetRedis)
app.Delete("/deletes", handlers.DeleteRedis)
config.ConnectMongoDB()
app.Get("/mongodb", handlers.GetMongoDB)
//登录
// GET /flights/LAX-SFO
app.Get("/login/:username/:pwd", handlers.ValidateUser)
//注册
app.Get("/login/:username/:pwd", handlers.SaveUser)
// GET /api/register
app.Get("/api/*", func(c *fiber.Ctx) error {
msg := fmt.Sprintf("✋ %s", c.Params("*"))
return c.SendString(msg) // => ✋ register
})
// GET /flights/LAX-SFO
app.Get("/flights/:from-:to", func(c *fiber.Ctx) error {
msg := fmt.Sprintf("💸 From: %s, To: %s", c.Params("from"), c.Params("to"))
return c.SendString(msg) // => 💸 From: LAX, To: SFO
})
// GET /dictionary.txt
app.Get("/:file.:ext", func(c *fiber.Ctx) error {
msg := fmt.Sprintf("📃 %s.%s", c.Params("file"), c.Params("ext"))
return c.SendString(msg) // => 📃 dictionary.txt
})
// GET /john/75
app.Get("/:name/:age/:gender?", func(c *fiber.Ctx) error {
msg := fmt.Sprintf("👴 %s is %s years old", c.Params("name"), c.Params("age"))
return c.SendString(msg) // => 👴 john is 75 years old
})
// GET /john
app.Get("/:name", func(c *fiber.Ctx) error {
msg := fmt.Sprintf("Hello, %s 👋!", c.Params("name"))
return c.SendString(msg) // => Hello john 👋!
})
app.Post("/post", func(c *fiber.Ctx) error {
payload := struct {
Name string `json:"name"`
Email string `json:"email"`
}{}
if err := c.BodyParser(&payload); err != nil {
return err
}
return c.JSON(payload)
})
app.Post("/post", func(c *fiber.Ctx) error {
user := new(entities.User)
if err := c.BodyParser(user); err != nil {
fmt.Println("error = ",err)
return c.SendStatus(200)
}
// getting user if no error
fmt.Println("user = ", user)
fmt.Println("user = ", user.Pwd)
fmt.Println("user = ", user.Username)
return c.SendString(user.Username)
})
// websocket 服务
app.Use("/ws", func(c *fiber.Ctx) error {
// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
if websocket.IsWebSocketUpgrade(c) {
c.Locals("allowed", true)
return c.Next()
}
return fiber.ErrUpgradeRequired
})
app.Get("/ws/:id", websocket.New(func(c *websocket.Conn) {
// c.Locals is added to the *websocket.Conn
log.Println(c.Locals("allowed")) // true
log.Println(c.Params("id")) // 123
log.Println(c.Query("v")) // 1.0
log.Println(c.Cookies("session")) // ""
// websocket.Conn bindings https://pkg.go.dev/github.com/fasthttp/websocket?tab=doc#pkg-index
var (
mt int
msg []byte
err error
)
for {
if mt, msg, err = c.ReadMessage(); err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", msg)
if err = c.WriteMessage(mt, msg); err != nil {
log.Println("write:", err)
break
}
}
}))
app.Get("/index.html", handlers.StaticHtml)
app.Post("/get", func(c *fiber.Ctx) error {
fmt.Println("c.get-appID", c.Get("X-Auth-AppId"), c.Get("X-Auth-Token"))
return c.SendString("Hello, World!")
})
app.Get("/index.html", handlers.DlpDemoCtx)
//打印屏幕输入数据
app.Put("/put", handlers.Reader)
data, _ := json.MarshalIndent(app.Stack(), "", " ")
fmt.Println(string(data))
// //执行xxl-job服务
app.Use("/xxlJob", handlers.XXLJob)
//延迟执行
time.AfterFunc(time.Second * 5, Get)
//监听端口
log.Fatal(app.Listen(":3000"))
}
/**
* gin框架
*/
func ginInit(){
r := gin.Default()
// 匹配/users/xxx
r.GET("/users/:name", func(c *gin.Context) {
name := c.Param("name")
c.String(http.StatusOK, "Hello %s\n", name)
})
// 带query参数
r.GET("/books", func(c *gin.Context) {
name := c.Query("name")
c.String(http.StatusOK, "finding for book: %s\n", name)
})
// 表单数据
r.POST("/register", func(c *gin.Context) {
email := c.PostForm("email")
password := c.DefaultPostForm("password", "123456")
c.JSON(http.StatusOK, gin.H {
"email": email,
"password": password,
})
})
// 分组
defaultHandler := func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"path": c.FullPath(),
})
}
// group: v1
v1 := r.Group("/v1")
{
v1.GET("/posts", defaultHandler)
v1.GET("/series", defaultHandler)
}
// group: v2
v2 := r.Group("/v2")
{
v2.GET("/posts", defaultHandler)
v2.GET("/series", defaultHandler)
}
// 模板渲染
//r.LoadHTMLGlob("templates/*")
r.GET("/students", func(c *gin.Context) {
stus := []entities.Student{
{ Name: "Jack", Age: 20 },
{ Name: "David", Age: 18 },
}
c.HTML(http.StatusOK, "arr.tmpl", gin.H{
"title": "Gin",
"stuArr": stus,
})
})
// listen and serve on 0.0.0.0:8080
r.Run(":8080")
}
// 触发xxl-job服务方法
func Get(){
fmt.Println("开启延时执行")
resp, err := http.Get("http://127.0.0.1:3000/xxlJob")
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
fmt.Println(resp.StatusCode)
if resp.StatusCode == 200 {
fmt.Println("ok")
}
}
package testing
import (
"fmt"
"strings"
"github.com/buckhx/gobert/tokenize"
"github.com/buckhx/gobert/tokenize/vocab"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/aclements/go-gg/generic/slice"
)
var corpus = `深度学习的基础是机器学习中的分散表示(distributed representation)。分散表示假定观测值是由不同因
子相互作用生成。在此基础上,深度学习进一步假定这一相互作用的过程可分为多个层次,代表对观测值的多层抽象。不同
的层数和层的规模可用于不同程度的抽象。深度学习运用了这分层次抽象的思想,更高层次的概念从低层次的概念学习得到。
这一分层结构常常使用贪心算法逐层构建而成,并从中选取有助于机器学习的更有效的特征。不少深度学习算法都以无监督
学习的形式出现,因而这些算法能被应用于其他算法无法企及的无标签数据,这一类数据比有标签数据更丰富,也更容易获
得。这一点也为深度学习赢得了重要的优势。`
var question = "深度学习的基础是什么?"
/* 判断是否是英文字符 */
func isAlpha(c byte) bool {
return (c>=65 && c<=90) || (c>=97 && c<=122)
}
func main() {
modelPath := "../../../nlp/albert_QA/outputs/saved-model"
vocabPath := "../../../nlp/nlp_model/albert_zh_base/vocab_chinese.txt"
voc, err := vocab.FromFile(vocabPath)
if err != nil {
panic(err)
}
m, err := tf.LoadSavedModel(modelPath, []string{"train"}, nil)
if err != nil {
panic(err)
}
tkz := tokenize.NewTokenizer(voc)
ff := tokenize.FeatureFactory{Tokenizer: tkz, SeqLen: 512}
// 拼接输入
input_tokens := question + tokenize.SequenceSeparator + corpus
// 获取 token 向量
f := ff.Feature(input_tokens)
tids, err := tf.NewTensor([][]int32{f.TokenIDs})
if err != nil {
panic(err)
}
new_mask := make([]float32, len(f.Mask))
for i, v := range f.Mask {
new_mask[i] = float32(v)
}
mask, err := tf.NewTensor([][]float32{new_mask})
if err != nil {
panic(err)
}
sids, err := tf.NewTensor([][]int32{f.TypeIDs})
if err != nil {
panic(err)
}
fmt.Println(f.Tokens)
//fmt.Println(f.TokenIDs)
//fmt.Println(f.Mask)
//fmt.Println(f.TypeIDs)
res, err := m.Session.Run(
map[tf.Output]*tf.Tensor{
m.Graph.Operation("input_ids").Output(0): tids,
m.Graph.Operation("input_mask").Output(0): mask,
m.Graph.Operation("segment_ids").Output(0): sids,
},
[]tf.Output{
m.Graph.Operation("finetune_mrc/Squeeze").Output(0),
m.Graph.Operation("finetune_mrc/Squeeze_1").Output(0),
},
nil,
)
if err != nil {
panic(err)
}
//fmt.Println("DataType", res[0].DataType())
//fmt.Println("Shape", res[0].Shape())
//fmt.Println("Value", res[0].Value().([][]float32))
st := slice.ArgMax(res[0].Value().([][]float32)[0])
ed := slice.ArgMax(res[1].Value().([][]float32)[0])
fmt.Println(len(f.Tokens), st, ed)
if ed<st{ // ed 小于 st 说明未找到答案
st = 0
ed = 0
}
//ans := strings.Join(f.Tokens[st:ed+1], "")
// 处理token中的英文,例如: 'di', '##st', '##ri', '##bu', '##ted', 're', '##pr', '##ese', '##nt', '##ation',
ans := ""
for i:=st;i<ed+1;i++ {
if len(f.Tokens[i])>0 && isAlpha(f.Tokens[i][0]){ // 英文开头,加空格
ans += " "+f.Tokens[i]
} else if strings.HasPrefix(f.Tokens[i], "##"){ // ##开头,是英文中段,去掉##
ans += f.Tokens[i][2:]
} else {
ans += f.Tokens[i]
}
}
if strings.HasPrefix(ans, "[CLS]") || strings.HasPrefix(ans, "[SEP]") {
fmt.Println("未找到答案。")
} else {
fmt.Println("Question: ", question)
fmt.Println("Answer: ", ans)
}
}
package testing
import (
"strings"
"github.com/buckhx/gobert/tokenize"
"github.com/buckhx/gobert/tokenize/vocab"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/aclements/go-gg/generic/slice"
)
const (
MaxSeqLength = 512
)
/* 训练好的模型权重 */
var m *tf.SavedModel
var voc vocab.Dict
func InitModel(modelPath string, vocabPath string) {
var err error
voc, err = vocab.FromFile(vocabPath)
if err != nil {
panic(err)
}
m, err = tf.LoadSavedModel(modelPath, []string{"train"}, nil)
if err != nil {
panic(err)
}
}
/* 判断是否是英文字符 */
func isAlphaQa(c byte) bool {
return (c>=65 && c<=90) || (c>=97 && c<=122)
}
func BertQA(corpus string, question string) (ans string, err error) {
tkz := tokenize.NewTokenizer(voc)
ff := tokenize.FeatureFactory{Tokenizer: tkz, SeqLen: MaxSeqLength}
// 拼接输入
input_tokens := question + tokenize.SequenceSeparator + corpus
// 获取 token 向量
f := ff.Feature(input_tokens)
tids, err := tf.NewTensor([][]int32{f.TokenIDs})
if err != nil {
return ans, err
}
new_mask := make([]float32, len(f.Mask))
for i, v := range f.Mask {
new_mask[i] = float32(v)
}
mask, err := tf.NewTensor([][]float32{new_mask})
if err != nil {
return ans, err
}
sids, err := tf.NewTensor([][]int32{f.TypeIDs})
if err != nil {
return ans, err
}
res, err := m.Session.Run(
map[tf.Output]*tf.Tensor{
m.Graph.Operation("input_ids").Output(0): tids,
m.Graph.Operation("input_mask").Output(0): mask,
m.Graph.Operation("segment_ids").Output(0): sids,
},
[]tf.Output{
m.Graph.Operation("finetune_mrc/Squeeze").Output(0),
m.Graph.Operation("finetune_mrc/Squeeze_1").Output(0),
},
nil,
)
if err != nil {
return ans, err
}
st := slice.ArgMax(res[0].Value().([][]float32)[0])
ed := slice.ArgMax(res[1].Value().([][]float32)[0])
if ed<st{ // ed 小于 st 说明未找到答案
st = ed
}
//ans = strings.Join(f.Tokens[st:ed+1], "")
// 处理token中的英文,例如: 'di', '##st', '##ri', '##bu', '##ted', 're', '##pr', '##ese', '##nt', '##ation',
ans = ""
for i:=st;i<ed+1;i++ {
if len(f.Tokens[i])>0 && isAlphaQa(f.Tokens[i][0]){ // 英文开头,加空格
ans += " "+f.Tokens[i]
} else if strings.HasPrefix(f.Tokens[i], "##"){ // ##开头,是英文中段,去掉##
ans += f.Tokens[i][2:]
} else {
ans += f.Tokens[i]
}
}
if strings.HasPrefix(ans, "[CLS]") || strings.HasPrefix(ans, "[SEP]") {
return "", nil
} else {
return ans, nil // 找到答案
}
}
package testing
import (
"C"
"encoding/json"
"fmt"
"reflect"
"strings"
"sync"
"testing"
"unsafe"
)
type fakeService struct {
}
type Post struct {
Name string
Address string
}
type Service interface {
ListPosts() ([]*Post, error)
}
func ListPosts(svc Service) ([]*Post, error) { return svc.ListPosts()}
func newFakeService() Service {
return &fakeService{}
}
func (s *fakeService) ListPosts() ([]*Post, error) {
posts := make([] *Post, 0)
posts = append(posts, &Post{
Name: "colin",
Address:"Shenzhen",
})
posts = append(posts, &Post{
Name:"alex",
Address:"beijing",
})
return posts, nil
}
func TestListPosts(t *testing.T){
fake := newFakeService()
if _, err := ListPosts(fake); err != nil {
t.Fatal("list posts failed")
}
}
type Bird interface {
Fly()
Type() string
}
type Canary struct {
Name string
}
func (c *Canary)Fly() {
fmt.Printf("我是%s,用黄色的翅膀飞\n", c.Name)
}
func (c *Canary) Type() string {
return c.Name
}
type Crow struct {
Name string
}
func (c *Crow) Fly() {
fmt.Printf("我是%s,我用黑色的翅膀飞\n", c.Name)
}
func (c *Crow) Type() string {
return c.Name
}
func LetFay(bird Bird) {
fmt.Printf("Let %s \n", bird.Type())
bird.Fly()
}
func testFay(t *testing.T) {
LetFay(&Canary{Name:"jj"})
LetFay(&Crow{Name:"gg"})
C.puts(C.CString("Hello, Cgo\n"))
cblob := C.blob{} // 在GO程序中创建的C对象,存储在Go的内存空间
cblob.repeat_time = 0
cblob.str = C.CString("Hello, World\n") // C.CString 会在C的内存空间申请一个C语言字符串对象,再将Go字符串拷贝到C字符串
ret := C.SayHello(&cblob) // &cblob 取C语言对象cblob的地址
fmt.Println("ret", ret)
fmt.Println("repeat_time", cblob.repeat_time)
C.free(unsafe.Pointer(cblob.str)) // C.CString 申请的C空间内存不会自动释放,需要显示调用C中的free释放
}
//问题很简单,使⽤ channel 来控制打印的进度。使⽤两个 channel ,来分别控制数字和
//字⺟的打印序列, 数字打印完成后通过 channel 通知字⺟打印, 字⺟打印完成后通知数
//字打印,然后周⽽复始的⼯作。
func testNumber(t *testing.T){
l,n:=make(chan bool), make(chan bool)
wait := sync.WaitGroup{}
go func() {
i:= 1
for {
select {
case <- n:
fmt.Print(i)
i++
fmt.Print(i)
i++
l <- true
break
default:
break
}
}
}()
wait.Add(1)
go func(wait *sync.WaitGroup) {
str := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
i:= 1
for {
select {
case <- l:
if i >= strings.Count(str, "")-1 {
wait.Done()
return
}
fmt.Print(str[i:i+1])
i++
if i >= strings.Count(str, "") {
i = 0
}
fmt.Print(str[i:i+1])
i++
n <- true
break
default:
break
}
}
}(&wait)
n <- true
wait.Wait()
}
//第⼀个⽅法使⽤的是golang内置⽅法 strings.Count ,可以⽤来判断在⼀个字符串中包含
//的另外⼀个字符串的数量。
func isUniqueString(s string) bool {
if strings.Count(s, "") > 3000 {
return false
}
for _,v := range s{
if v > 127 {
return false
}
if strings.Count(s, string(v)) > 1 {
return false
}
}
return true
}
//第⼆个⽅法使⽤的是golang内置⽅法 strings.Index 和 strings.LastIndex ,⽤来判断指
//定字符串在另外⼀个字符串的索引未知,分别是第⼀次发现位置和最后发现位置。
func isUniqueString2(s string) bool {
if strings.Count(s, "") > 3000 {
return false
}
for k,v := range s{
if v > 127 {
return false
}
if strings.Index(s, string(v)) != k {
return false
}
}
return true
}
//func reverString2(s string) (string, bool) {
// str := [] rune(s)
// l := len(str)
// if len > 5000 {
// return s, false
// }
// for i := 0; i< len/2 ; i++ {
// str[i], str[l-1-i] = str[l-1-i], str[i]
// }
// return string(str), true
//}
//在json的规范中,对于数字类型是不区分整形和浮点型的。
//上游将id改为string传给下游
//下游使用json.number类型来避免对float64的使用
func dealStrTest(t *testing.T){
var request = `{"id":7044144249855934983,"name":"demo"}`
var test interface{} // 不指定反序列化的类型
err := json.Unmarshal([]byte(request), &test)
if err != nil {
fmt.Println("error:", err)
}
obj := test.(map[string]interface{})
dealStr, err := json.Marshal(test)
if err != nil {
fmt.Println("error:", err)
}
id := obj["id"]
// 反序列化之后重新序列化打印
fmt.Println(string(dealStr))
fmt.Printf("%+v\n", reflect.TypeOf(id).Name())
fmt.Printf("%+v\n", id.(float64))
}
//饿汉模式
type singleton struct {}
var ins *singleton = &singleton{}
var mu sync.Mutex
var once sync.Once
func GetInsOr() *singleton {
if ins == nil {
mu.Lock()
if ins == nil {
ins = &singleton{}
}
mu.Unlock()
}
return ins
}
//推荐 使用once.Do可以确保 ins 实例全局只被创建一次,once.Do 函数还可以确保当同时有多个创建动作时,只有一个创建动作在被执行。
func GetIns() *singleton {
once.Do(func() {
ins = &singleton{}
})
return ins
}
//工厂模式
//type Person struct {
// Name string
// Age int
//}
//
//func (p Person) Greet() {
// fmt.Printf("Hi! My name is %s", p.Name)
//}
//
//func NewPerson(name string, age int) *Person {
// return &Person{
// Name: name,
// Age: age,
// }
//}
//抽象工厂模式
//type Person interface {
// Greet()
//}
//
//type person struct {
// name string
// age int
//}
//
//func (p person) Greet() {
// fmt.Printf("Hi! My name is %s", p.name)
//}
//
//// Here, NewPerson returns an interface, and not the person struct itself
//func NewPerson(name string, age int) Person {
// return person{
// name: name,
// age: age,
// }
//}
type Person struct {
name string
age int
}
func NewPersonFactory(age int) func(name string) Person {
return func(name string) Person {
return Person{
name: name,
age: age,
}
}
}
func NewTest(){
newBaby := NewPersonFactory(1)
_ = newBaby("john")
newTeenager := NewPersonFactory(16)
_ = newTeenager("jill")
}
//type Doer struct {
// name string
//}
//
//func QueryUser(doer Doer) error {
// req, err := http.NewRequest("Get", "http://iam.api.marmotedu.com:8080/v1/secrets", nil)
// if err != nil {
// return err
// }
// _, err := doer.Do(req)
// if err != nil {
// return err
// }
//
// return nil
//}
//
//
//func TestQueryUser(t *testing.T) {
// doer := NewMockHTTPClient()
// if err := QueryUser(doer); err != nil {
// t.Errorf("QueryUser failed, err: %v", err)
// }
//}
// 策略模式
// 定义一个策略类
type IStrategy interface {
do(int, int) int
}
// 策略实现:加
type add struct{}
func (*add) do(a, b int) int {
return a + b
}
// 策略实现:减
type reduce struct{}
func (*reduce) do(a, b int) int {
return a - b
}
// 具体策略的执行者
type Operator struct {
strategy IStrategy
}
// 设置策略
func (operator *Operator) setStrategy(strategy IStrategy) {
operator.strategy = strategy
}
// 调用策略中的方法
func (operator *Operator) calculate(a, b int) int {
return operator.strategy.do(a, b)
}
package testing
import (
tf "github.com/tensorflow/tensorflow/tensorflow/go"
"github.com/tensorflow/tensorflow/tensorflow/go/op"
"fmt"
)
func testTf() {
// Construct a graph with an operation that produces a string constant.
s := op.NewScope()
c := op.Const(s, "Hello from TensorFlow version " + tf.Version())
graph, err := s.Finalize()
if err != nil {
panic(err)
}
// Execute the graph in a session.
sess, err := tf.NewSession(graph, nil)
if err != nil {
panic(err)
}
output, err := sess.Run(nil, []tf.Output{c}, nil)
if err != nil {
panic(err)
}
fmt.Println(output[0].Value())
}
//go 调用 tensorflow模型
//参考 https://blog.csdn.net/cdj0311/article/details/81773849
// 参考 https://github.com/jack139/gotf
func model() {
// 句子最大长度
const MAXLEN int = 20
// 将文本转换为id序列,为了实验方便直接使用转换好的ID序列即可
input_data := [1][MAXLEN]float32{{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 208.0, 659.0, 180.0, 408.0, 42.0, 547.0, 829.0, 285.0, 334.0, 42.0, 642.0, 81.0, 800.0}}
tensor, err := tf.NewTensor(input_data)
if err != nil {
fmt.Printf("Error NewTensor: err: %s", err.Error())
return
}
//读取模型
model, err := tf.LoadSavedModel("cnnModel", []string{"myTag"}, nil)
if err != nil {
fmt.Printf("Error loading Saved Model: %s\n", err.Error())
return
}
// 识别
result, err := model.Session.Run(
map[tf.Output]*tf.Tensor{
// python版tensorflow/keras中定义的输入层input_layer
model.Graph.Operation("input_layer").Output(0): tensor,
},
[]tf.Output{
// python版tensorflow/keras中定义的输出层output_layer
model.Graph.Operation("output_layer/Softmax").Output(0),
},
nil,
)
if err != nil {
fmt.Printf("Error running the session with input, err: %s ", err.Error())
return
}
// 输出结果,interface{}格式
fmt.Printf("Result value: %v", result[0].Value())
}
module github.com/programmerug/fibergorm
go 1.16
require (
github.com/aclements/go-gg v0.0.0-20170323211221-abd1f791f5ee
github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/apache/rocketmq-client-go/v2 v2.1.0
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/blugelabs/bluge v0.1.9
github.com/buckhx/gobert v0.0.0-20190731141805-265756fed232
github.com/bytedance/godlp v1.2.15
github.com/fasthttp/websocket v1.4.5 // indirect
github.com/fatih/set v0.2.1
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gin-gonic/gin v1.6.3
github.com/go-redis/redis/v8 v8.11.4
github.com/go-stack/stack v1.8.1 // indirect
github.com/gofiber/fiber/v2 v2.24.0
github.com/gofiber/websocket/v2 v2.0.15
github.com/google/go-querystring v1.1.0
github.com/kardianos/service v1.2.0
github.com/klauspost/compress v1.14.1 // indirect
github.com/olivere/elastic/v7 v7.0.31
github.com/pkg/errors v0.9.1
github.com/pyroscope-io/pyroscope v0.7.2
github.com/satori/go.uuid v1.2.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/tensorflow/tensorflow v1.15.4
github.com/tidwall/gjson v1.12.1 // indirect
github.com/xxl-job/xxl-job-executor-go v1.1.0
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.mongodb.org/mongo-driver v1.8.2
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
golang.org/x/net v0.0.0-20220111093109-d55c255bac03 // indirect
golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gorm.io/driver/mysql v1.2.3
gorm.io/gorm v1.22.5
gorm.io/hints v1.1.0
)