From d9bdfaf2ef0114d74e3b923a2b45d69a9cae7ffd Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Fri, 30 Apr 2021 15:47:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=BA=9B=E6=B5=8B=E8=AF=95=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/ip.go | 14 ++++++++++-- tools/ip_test.go | 11 ++++++++++ tools/mytest.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ tools/mytest_test.go | 49 +++++++++++++++++++++++++++++++++++++++++ tools/rpc_test.go | 13 +++++++++++ tools/smtp.go | 3 ++- tools/smtp_test.go | 8 +++++++ tools/sorts.go | 15 +++++++++++++ tools/test.go | 12 +--------- tools/test_test.go | 1 + 10 files changed, 164 insertions(+), 14 deletions(-) create mode 100644 tools/ip_test.go create mode 100644 tools/mytest.go create mode 100644 tools/mytest_test.go create mode 100644 tools/rpc_test.go create mode 100644 tools/smtp_test.go create mode 100644 tools/test_test.go diff --git a/tools/ip.go b/tools/ip.go index 17998ee..5c79724 100644 --- a/tools/ip.go +++ b/tools/ip.go @@ -62,6 +62,16 @@ func getIpFromAddr(addr net.Addr) net.IP { return ip } -func GetExternalIp() string { - return Get("http://myexternalip.com/raw") + +//获取出站IP地址 +func GetOutboundIP() (net.IP, error) { + conn, err := net.Dial("udp", "8.8.8.8:80") + if err != nil { + return nil, err + } + defer conn.Close() + + localAddr := conn.LocalAddr().(*net.UDPAddr) + + return localAddr.IP, nil } diff --git a/tools/ip_test.go b/tools/ip_test.go new file mode 100644 index 0000000..fe1e824 --- /dev/null +++ b/tools/ip_test.go @@ -0,0 +1,11 @@ +package tools + +import ( + "log" + "testing" +) + +func TestGetOutboundIP(t *testing.T) { + ip, err := GetOutboundIP() + log.Println(ip, err) +} diff --git a/tools/mytest.go b/tools/mytest.go new file mode 100644 index 0000000..bd3bba9 --- /dev/null +++ b/tools/mytest.go @@ -0,0 +1,52 @@ +package tools + +import "fmt" + +func MyTest() { + type MConn struct { + Name string + } + var conn *MConn + var conn2 MConn + conn3 := new(MConn) + conn4 := &MConn{} + fmt.Printf("%v,%v,%v,%v \r\n", conn, conn2, conn3, conn4) + + var mMap map[string][]*MConn + m1, _ := mMap["name"] + //if ok { + // m1.Name = "qqq" + //} + fmt.Printf("ssss%T", m1) +} +func MyStruct() { + type s2 struct { + name string + } + aa := s2{ + name: "aa", + } + bb := s2{ + name: "aa", + } + fmt.Printf("%v\n", aa == bb) + + type s1 struct { + one map[string]string + two []string + three string + } + + a := &s1{ + one: map[string]string{"aaa": "bbb"}, + two: []string{"aaa", "bbb"}, + three: "aaaa", + } + b := &s1{ + one: map[string]string{"aaa": "bbb"}, + two: []string{"aaa", "bbb"}, + three: "aaaa", + } + c := a + fmt.Printf("%v;%v", a == b, a == c) +} diff --git a/tools/mytest_test.go b/tools/mytest_test.go new file mode 100644 index 0000000..8bdfa4f --- /dev/null +++ b/tools/mytest_test.go @@ -0,0 +1,49 @@ +package tools + +import ( + "sync" + "testing" +) + +func TestMyTest(t *testing.T) { + MyTest() +} +func TestMyStruct(t *testing.T) { + MyStruct() +} + +type SMap struct { + sync.RWMutex + Map map[int]int +} + +func (l *SMap) readMap(key int) (int, bool) { + l.RLock() + value, ok := l.Map[key] + l.RUnlock() + return value, ok +} + +func (l *SMap) writeMap(key int, value int) { + l.Lock() + l.Map[key] = value + l.Unlock() +} + +var mMap *SMap + +func TestMyMap(t *testing.T) { + mMap = &SMap{ + Map: make(map[int]int), + } + + for i := 0; i < 10000; i++ { + go func() { + mMap.writeMap(i, i) + }() + go readMap(i) + } +} +func readMap(i int) (int, bool) { + return mMap.readMap(i) +} diff --git a/tools/rpc_test.go b/tools/rpc_test.go new file mode 100644 index 0000000..5f4b9fd --- /dev/null +++ b/tools/rpc_test.go @@ -0,0 +1,13 @@ +package tools + +import ( + "go-fly-muti/frpc" + "testing" +) + +func TestClientRpc(t *testing.T) { + frpc.ClientRpc() +} +func TestServerRpc(t *testing.T) { + frpc.NewRpcServer("127.0.0.1:8082") +} diff --git a/tools/smtp.go b/tools/smtp.go index 8e0ac04..2fabcfe 100644 --- a/tools/smtp.go +++ b/tools/smtp.go @@ -14,7 +14,8 @@ func SendSmtp(server string, from string, password string, to []string, subject "From: " + from + "\r\n" + "To: " + strings.Join(to, ",") + "\r\n" + "Subject: =?UTF-8?B?" + subjectBase + "?=\r\n" + - "\r\n" + + "Content-Type: text/html; charset=UTF-8" + + "\r\n\r\n" + body + "\r\n") err := smtp.SendMail(server, auth, from, to, msg) if err != nil { diff --git a/tools/smtp_test.go b/tools/smtp_test.go new file mode 100644 index 0000000..ae5b99a --- /dev/null +++ b/tools/smtp_test.go @@ -0,0 +1,8 @@ +package tools + +import "testing" + +func TestSendSmtp(t *testing.T) { + body := "hello" + SendSmtp("smtp.sina.cn:25", "taoshihan1@sina.com", "382e8a5e11cfae8c", []string{"taoshihan1@sina.com"}, "123456", body) +} diff --git a/tools/sorts.go b/tools/sorts.go index 8969281..10a33d9 100644 --- a/tools/sorts.go +++ b/tools/sorts.go @@ -1,5 +1,20 @@ package tools +import "sort" + +func SortMap(youMap map[string]interface{}) []interface{} { + keys := make([]string, 0) + for k, _ := range youMap { + keys = append(keys, k) + } + myMap := make([]interface{}, 0) + sort.Strings(keys) + for _, k := range keys { + myMap = append(myMap, youMap[k]) + } + return myMap +} + //划分 func partition(arr *[]int, left int, right int) int { privot := (*arr)[right] diff --git a/tools/test.go b/tools/test.go index 3b132ff..7efbc5a 100644 --- a/tools/test.go +++ b/tools/test.go @@ -1,15 +1,5 @@ package tools -import ( - "fmt" - "mime" -) +func MyPointer() { -func main() { - dec := new(mime.WordDecoder) - header, err := dec.DecodeHeader("=?utf-8?q?=C3=89ric?= , =?utf-8?q?Ana=C3=AFs?= ") - if err != nil { - panic(err) - } - fmt.Println(header) } diff --git a/tools/test_test.go b/tools/test_test.go new file mode 100644 index 0000000..f7eca12 --- /dev/null +++ b/tools/test_test.go @@ -0,0 +1 @@ +package tools