This commit is contained in:
aozhiwei 2020-12-19 16:47:12 +08:00
parent 03ffea28fe
commit bc3b2825a6
4 changed files with 76 additions and 8 deletions

View File

@ -67,15 +67,17 @@ func HttpGetAsJson(url string, params *XObject) (*XObject, string, error) {
return respObj, respStr, err
}
func HttpPostContent(url string, contentType string, body string) (string, error) {
func HttpPostContent(url string, contentType string, body string, response *string) (error) {
*response = ""
resp, err := http.Post(url, contentType, strings.NewReader(body))
if err != nil {
return "", err
return err
}
defer resp.Body.Close()
if bytes, err := ioutil.ReadAll(resp.Body); err == nil {
return string(bytes), nil
*response = string(bytes)
return nil
} else {
return "", err
return err
}
}

View File

@ -1,5 +1,7 @@
package q5
type ListHead_Foreach_Func func(interface{}) bool
type ListHead struct {
next *ListHead
prev *ListHead
@ -27,6 +29,16 @@ func (this *ListHead) AddTail(pnew *ListHead) {
prev.next = pnew
}
func (this *ListHead) AddHead(pnew *ListHead) {
prev := this
next := this.prev
next.prev = pnew
pnew.next = next
pnew.prev = prev
prev.next = pnew
}
func (this *ListHead) FirstEntry() interface{} {
return this.next.data
}
@ -53,3 +65,38 @@ func (this *ListHead) DelInit() {
this.next = this
this.prev = this
}
func (this *ListHead) Next() *ListHead {
return this.next
}
func (this *ListHead) ForEachFrom(from *ListHead,
cb ListHead_Foreach_Func) {
for pos := from; pos != this; pos = pos.next {
if !cb(pos.data) {
break
}
}
}
func (this *ListHead) ForEach(cb ListHead_Foreach_Func) {
for pos := this.next; pos != this; pos = pos.next {
if !cb(pos.data) {
break
}
}
}
func (this *ListHead) ForEach_r(cb ListHead_Foreach_Func) {
for pos := this.prev; pos != this; pos = pos.prev {
if !cb(pos.data) {
break
}
}
}
func MakeListHead () *ListHead{
l := new(ListHead)
l.Init(nil)
return l
}

View File

@ -1,6 +1,7 @@
package q5
import (
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
@ -23,7 +24,13 @@ func (this* Mysql) init(host string, port int32, user string, passwd string, dat
}
func (this* Mysql) Open() error {
db, err := sql.Open("mysql", "")
connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8",
this.user,
this.passwd,
this.host,
this.port,
this.database)
db, err := sql.Open("mysql", connStr)
this.db = db
return err
}
@ -39,16 +46,16 @@ func (this* Mysql) Ping() error {
}
func (this* Mysql) Query(query string, args ...interface{}) (*sql.Rows, error) {
rows, err := this.db.Query(query, args)
rows, err := this.db.Query(query, args...)
return rows, err
}
func (this* Mysql) QueryRow(query string, args ...interface{}) *sql.Row {
return this.db.QueryRow(query, args)
return this.db.QueryRow(query, args...)
}
func (this* Mysql) Exec(query string, args ...interface{}) (sql.Result, error) {
result, err := this.db.Exec(query, args)
result, err := this.db.Exec(query, args...)
return result, err
}

View File

@ -6,6 +6,7 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"encoding/base64"
"hash/crc32"
)
@ -21,6 +22,17 @@ func Md5Str(data string) string {
return hex.EncodeToString(h.Sum(nil))
}
func Base64Encode(data string) string {
strbytes := []byte(data)
encoded := base64.StdEncoding.EncodeToString(strbytes)
return encoded
}
func Base64Decode(data string) (string, error) {
decoded, err := base64.StdEncoding.DecodeString(data)
return string(decoded), err
}
func Crc32(data string) uint32 {
ieee := crc32.NewIEEE()
io.WriteString(ieee, data)