golang Golang - 如何使用MD5散列字符串。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang Golang - 如何使用MD5散列字符串。相关的知识,希望对你有一定的参考价值。

import (
    "crypto/md5"
    "encoding/hex"
)

func GetMD5Hash(text string) string {
    hasher := md5.New()
    hasher.Write([]byte(text))
    return hex.EncodeToString(hasher.Sum(nil))
}

markdown Golang tutorials.md

# INTRODUCTION

## Workspaces

Sẽ có 2 kiểu program trong Go:
* **Executables**: là chương trình có thể chạy **trực tiếp** từ terminal.
* **Libraries**: đóng gói thành package để các chương trình khác có thể gọi tới.
Basically, Go workspace contains three directories:
* `src`: Contains Go source files.
* `pkg`: Contains package objects
* `bin`: Contains Go executable commands.


We will keep source codes in directory named `$GOPATH/src/github.com/user/project1` so we can call our package in another one by using:
`import ("github.com/user/project1")`

In order to build and install a package, you run:
`go install $GOPATH/src/github.com/user/project1` or more simply `go install github.com/user/project1`

The command builds the package and produces a executable command in our workspace's `bin` directory.

## About package

**Go package name is the last element in import path, an executable command must always use `package main`**, for example the `"math/rand"` package will contains a file start with `package rand`.

### Exported Names
In Go, a name is exported if it begins with capital letter, for example `Pi` in `math` package.

When importing packages, you can refer only to **package's exported name**, any unexported name can only be accessible inside the package.


## Go Declaration Syntax

### Variables
`var <variable_name> <type>`
  Examples:
  ```
  var x1, x2 int = 1, 2
  var y *int
  var z [3]int
  ```
  
### Function
`func <function_name>(args1 type1, args2 type2) <function_type>`
  Examples:
  ```
  func main(x int, str []string) int
  func main(int, []string) int
  ```
  
  **Notes:**
  * **Inside** a function `:=` can be used to replace `var` keyword.
  
### Pointer
* Pointer holds memory address of a value.
* Pointer have zero value is **nil**
  Examples:
  ```
  var p *int
  i := 42
  p = &i
  fmt.Println(*p) // read i through the pointer p
  *p = 21         // set i through the pointer p
  ```

### Struct

 Example:
 ```
 type Vertex struct {
	 X int
	 Y int
 }

 func main() {
	 v := Vertex{1, 2}
	 v.X = 4
	 fmt.Println(v.X)
 }
 ```

### Slice
* Declare a slice:
`letters := []string{"a", "b", "c", "d"}`

* Declare a slice using `make` func (`func make([]T, len, cap) []T)`: 
```
s = make([]byte, 5, 5)
// s == []byte{0, 0, 0, 0, 0}
```



### Type Conversion
  ```
  var x int = 10
  var y float64 = float64(x)
  var z uint = uint(y)
  ```
  
### Constant
  Examples:
  ```
  const Pi = 3.14
  ```
  **Constant cannot be declared using the `:=` syntax**
  
### Loop
 Examples:
 ```
 //For loop
 sum := 0
 for i := 0; i < 1000; i ++ {
  sum += i
 }
 
 //While loop
 sum := 0
 for sum < 1000 {
  sum += sum
 }
 
 //Infinity loop
 for {
  //Do sth
 }
 
 //Switch
 t := time.Now()
	switch {
	case t.Hour() < 12:
		fmt.Println("Good morning!")
	case t.Hour() < 17:
		fmt.Println("Good afternoon.")
	default:
		fmt.Println("Good evening.")
	}
 ```
### Defer
* `defer` ensure a function be performed **after** its surrounding function returns, usually `defer` is used as cleanup actions.
* Defer stack uses *last-in-first-out* order, for example:
 ```
 func b() {
    for i := 0; i < 4; i++ {
        defer fmt.Print(i)
    }
 }
 ```
 This will returns "3210"
 
 Example:
 ```
 f := createFile("/tmp/defer.txt")
 defer closeFile(f)
 writeFile(f)

 ```


[Go futher](https://golang.org/doc/code.html)
[Go programming language specifications](https://golang.org/ref/spec)
[Effective Go](https://golang.org/doc/effective_go.html)

以上是关于golang Golang - 如何使用MD5散列字符串。的主要内容,如果未能解决你的问题,请参考以下文章

golang Golang - 如何使用MD5散列字符串。

golang Golang - 如何使用MD5散列字符串。

Golang加密md5

Golang 中的 Bcrypt 密码散列(与 Node.js 兼容)?

golang MD5

golang 中的md5 hmacsha1算法的简单实现