Overview
learn some basic grammar, I began to learn to write a function, a function to share a few of his own writing.
MD5
Get current timestamp
acquires the current time string
generates a signature
function definition
FUNCTION_NAME FUNC (Type1 INPUT1, INPUT2 Type2) (Type1, Type2) { // function body // return multiple values return VALUE1, value2 }
Function with func statement.
Function can have one or more parameters, parameter types needed, with segmentation.
Function may return one or more values, the need for the return type, with segmentation.
Parameter is optional function, the return value is optional.
Value passed
when the transfer parameters, the parameters passed to the copy function, after adjusting the parameters, the parameter values do not affect.
Go default language is passed by value.
Passed by reference
when parameters are passed, the address parameter is passed to a function, after adjusting the parameters, the influence parameter.
This is Go - example Struct structure:
//demo_13.go package main import ( "encoding/json" "fmt" ) type Result struct { Code int `json:"code"` Message string `json:"msg"` } func main() { var res Result res.Code = 200 res.Message = "success" toJson(&res) setData(&res) toJson(&res) } func setData (res *Result) { res.Code = 500 res.Message = "fail" } func toJson (res *Result) { jsons, errs := json.Marshal(res) if errs != nil { fmt.Println("json marshal error:", errs) } fmt.Println("json data :", string(jsons)) }
Operating results:
MD5
// MD5 方法 func MD5(str string) string { s := md5.New() s.Write([]byte(str)) return hex.EncodeToString(s.Sum(nil)) } str := "12345" fmt.Printf("MD5(%s): %s\n", str, MD5(str))
The result:
get the current time string
// xxxx-xx-xx xx:xx:xx func getTimeStr() string { return time.Now().Format("2006-01-02 15:04:05") } fmt.Printf("current time str : %s\n", getTimeStr())
The result:
get the current timestamp
// 获取当前时间戳 func getTimeInt() int64 { return time.Now().Unix() } fmt.Printf("current time str : %s\n", getTimeStr()) fmt.Printf("current time unix : %d\n", getTimeInt())
The result:
generating a signature
//demo_26.go package main import ( "crypto/md5" "encoding/hex" "fmt" "sort" ) func main() { params := map[string]interface{} { "name" : "Tom", "pwd" : "123456", "age" : 30, } fmt.Printf("sign : %s\n", createSign(params)) } // MD5 方法 func MD5(str string) string { s := md5.New() s.The Write ([] byte (STR)) // signature is generated return hex.EncodeToString(s.Sum(nil)) } func createSign(params map[string]interface{}) string { var key []string var str = "" for k := range params { key = append(key, k) } sort.Strings(key) for i := 0; i < len(key); i++ { if i == 0 { str = fmt.Sprintf("%v=%v", key[i], params[key[i]]) } else { str = str + fmt.Sprintf("&xl_%v=%v", key[i], params[key[i]]) } } // 自定义密钥 var secret = "123456789" // Custom Signature Algorithm sign := MD5(MD5(str) + MD5(secret)) return sign }
The result:
the corresponding PHP to generate a signature method:
Interested parties can in the signature process, increasing the time stamp and secret reading in the configuration file.