golang版本:1.15

go代码

主程序

/* -*- coding:utf-8 -*-
author: cyz
time: 2022/1/5 15:32
*/

package main

import (
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis"
	"os/exec"
	"src/internal/pkg/login"
	"strings"
)


func runPythonFileInCMD(py_file_path string,args ...string) interface{} {
	//py_file_path = "E:/Program/GoProject/src/internal/pkg/login/sql_login.py"
	py_file_name := strings.Split(py_file_path,"/")[len(strings.Split(py_file_path,"/")) -1] //切分字符与list切片
	program_path := strings.Replace(py_file_path,py_file_name,"",-1) // 替换字符
	//attributes := "" //默认参数
	//if len(args) > 0 {
	//	attributes = `"` + strings.Join(args[:], `" "`) + `"`
	//}
	//command := fmt.Sprintf(`%s  %s `,py_file_name, attributes) //字符format方法
	command := append([]string{py_file_name}, args...)
	cmd := exec.Command("python",command...) // cmd命令使用方法
	cmd.Dir = program_path                     // cmd命令执行根目设置
	out, err := cmd.Output()                  // cmd命令执行
	if err != nil {                           // 返回结果判读
		fmt.Println("Error: ", err)
	}
	result := string(out)
	fmt.Println(result)
	var dat interface{}
	err = json.Unmarshal([]byte(result), &dat)
	if err == redis.Nil || err != nil {
		fmt.Println("转换成json错误: : ",err)
		return nil
	}
	return dat
}

//

func md5V(str string) string  {
	h := md5.New()
	h.Write([]byte(str))
	return hex.EncodeToString(h.Sum(nil))
}

func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {

		db, err := login.Engine_3308()
		if err != nil {
			fmt.Println("数据库链接错误", err)
		}
		//defer db.Close() // 延时关闭
		stmtOut := db.QueryRow("SELECT VERSION()")
		db.Close()
		var doc string
		stmtOut.Scan(&doc)

		c.JSON(200, gin.H{
			"mysql VERSION": doc,
		})
	})

	r.GET("/addr", func(c *gin.Context) {
		addr := c.DefaultQuery("addr","")
		GCJ02 :=runPythonFileInCMD("E:/Program/projects_etl/api封装层容器/src/空间类/地址经纬度返回.py", addr)
		//GCJ02 :=runPythonFileInCMD("E:/Program/GoProject/src/internal/pkg/login/sql_login.py", "",addr)

		c.JSON(200, gin.H{
			"addr": addr,
			"GCJ02":GCJ02,
		})
	})


	//r.GET("/addr", func(c *gin.Context) {
	//	addr := c.Query("addr")
	//	addr_md5 := md5V(addr)
	//
	//	c.JSON(200, gin.H{
	//		"message": addr,
	//		"addr_md5":addr_md5,
	//	})
	//})

	r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
	// http://localhost:8080/addr?addr=深圳南山

}


login_mysql

package login

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql" //导入mysql包
)

// 通过首字母大小写来控制的(包括函数和变量),大写表示可以被其他包访问,小写则不可以。
func Engine_3308() (conn *sql.DB,err error)  {
	host := "1.1.1.1"
	user := "user"
	password := "password"
	db := "db_test"
	port := 3308
	charset := "utf8mb4"
	data_source_name := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True",
		user,password,host,port,db,charset)
	conn, err = sql.Open("mysql", data_source_name)
	if err != nil {
		fmt.Println("数据库链接错误", err)
		return nil, err
	}
	//fmt.Println(reflect.TypeOf(db).String()) // 查询变量类型
	return conn, err
}

redis_login

/* -*- coding:utf-8 -*-
author: cyz
time: 2022/1/13 9:54
*/

package login

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"
	"github.com/go-redis/redis"
)

var (
	client = &redisCluterClient{}
)

//RedisClusterClient struct
type redisCluterClient struct {
	c *redis.ClusterClient
}

//GetClient get the redis client
func initialize(hostnames string) *redisCluterClient {
	addr := strings.Split(hostnames, ",")
	c := redis.NewClusterClient(&redis.ClusterOptions{
		Addrs: addr,
	})
	if err := c.Ping().Err(); err != nil {
		panic("Unable to connect to redis " + err.Error())
	}
	client.c = c
	return client
}

//GetKey get key
//func (client *redisCluterClient) getKey(key string, src interface{}) error {
//	val, err := client.c.Get(key).Result()
//	if err == redis.Nil || err != nil {
//		return err
//	}
//	err = json.Unmarshal([]byte(val), &src)
//	if err != nil {
//		return err
//	}
//	return nil
//}

//SetKey set key
//func (client *redisCluterClient) setKey(key string, value interface{}, expiration time.Duration) error {
//	cacheEntry, err := json.Marshal(value)
//	if err != nil {
//		return err
//	}
//	err = client.c.Set(key, cacheEntry, expiration).Err()
//	if err != nil {
//		return err
//	}
//	return nil
//}

type valueEx struct {
	Name  string
	Email string
}

//	a,_ :=json.Marshal("value")
//	typeOfA := reflect.TypeOf(a)
//	fmt.Println(typeOfA.Name(), typeOfA.Kind())
func ConnectRedis(hostnames string) *redisCluterClient{
	redisCluterClient := initialize(hostnames)
	return redisCluterClient
}

func (client *redisCluterClient) getKey(key string) (src interface{},err error){
	val, err := client.c.Get(key).Result()
	//fmt.Println(reflect.TypeOf(val).String())
	if err == redis.Nil || err != nil {
		fmt.Println("redis获取数据错误: ",err)
		return nil, err
	}
	err = json.Unmarshal([]byte(val), &src)
	if err == redis.Nil || err != nil {
		fmt.Println("转换成json错误: : ",err)
		return nil, err
	}
	return src, err
}

func (client *redisCluterClient) setKey(key string, value interface{}, expiration time.Duration) error {
	cacheEntry, err := json.Marshal(value)
	if err != nil {
		return err
	}
	err = client.c.Set(key, cacheEntry, expiration).Err()
	if err != nil {
		return err
	}
	return nil
}

//func main() {
//	//Use your actually ip address here
//	//redisCluterClient := initialize("1.1.1.1:0000,1.1.1.1:0001,1.1.1.1:0002,")
//	//redisCluterClient := initialize("1.1.1.1:0000")
//	//key1 := "manager-system:test-golang-login"
//	//value1 := &valueEx{Name: "someName", Email: "someemail@abc.com"}
//	//err := redisCluterClient.setKey(key1, value1, time.Minute*1)
//	//if err != nil {
//	//	log.Fatalf("Error: %v", err.Error())
//	//}
//	//value2 := &valueEx{}
//	//err = redisCluterClient.getKey(key1, value2)
//	//if err != nil {
//	//	log.Fatalf("Error: %v", err.Error())
//	//}
//	//log.Printf("Name: %s", value2.Name)
//	//log.Printf("Email: %s", value2.Email)
//
//	cr := ConnectRedis("1.1.1.1:0000")
//	key1 := "manager-system:test-golang-login"
//	value1 := &valueEx{Name: "someName", Email: "someemail@abc.com"}
//	err := cr.setKey(key1, value1, time.Minute*1)
//	if err != nil {
//		fmt.Println(err)
//	}
//	val, err :=cr.getKey(key1)
//	fmt.Println(val, err)
//
//}

使用goland生成到linux运行文件。

GOARCH=amd64;GOOS=linux