Some GoLang string using summary

In the project which should be the most exposed to the string, and there are, for example, received a request sent by the front desk at the time of writing API, most of which I believe are strings, we followed it for a string of some processing GoLang to be a small sum.

 

First, the format of the output string

package main
import "fmt"
func main() { fmt.Println("Hello") } 

I believe this is the first entry for all input of a code, simply enough, we are a little deeper.

package main
import "fmt"
func main() { fmt.Printf("Hi, my name is %v","BoB") } 

When we need to insert the character output of which some of the content they want to add, and then use the formatting, and other C language and what is similar, and here I have listed what these verbs and functions of specific parameters:

verb Features
% v Originally value of the output value in accordance with
% + V % V on the basis of a structure field names and values ​​are expanded
% # V Go output value of language syntax
%T Go language output - the type and value syntax
%% Output% body
%b Integer displayed in binary mode
%。 Integer displayed in octal way
%d Integer displayed in decimal way
%x Integer displayed in hexadecimal mode
%X Integer displayed in hexadecimal, uppercase letters
% U Unicode character
%f Float
%p Pointer, displayed in hexadecimal

Some of us are not commonly used, you can not remember him, it can also be used in the investigation again.

Second, the character string type conversion

When we receive a request sent by the client, most of the data we are required to use secondary treatment, such as the string turn int, turn int64, etc. Next Go inside to see how we turn.

    AByString := "1"
	BByInt := 2
	
	//类型转换成int CByInt,err := strconv.Atoi(AByString) if err != nil { fmt.Errorf("类型转换出错 %v",err) } fmt.Println(BByInt + CByInt) 

It's stringturn int.

    AByString := "1"
    
    //字符串转int64
	DByInt64, err := strconv.ParseInt(AByString, 10, 64) //int64转string EByString := strconv.FormatInt(DByInt64, 10) EByInt64, err := strconv.ParseInt(EByString, 10, 64) fmt.Println(EByInt64 + DByInt64) 

Here we used:

  • string turn int64
  • int64 turn string

strconvPackage there are many API is used to convert data types, not list them here, and our common type conversions which have been included.

Third, decrypting the encrypted string

Encryption and decryption are essential items inside, such as data transmission, if transmitted in clear text is a very terrible thing, I'll share two common encryption algorithm:

Base64

The first is a reversible encryption algorithm base64, Go language provides encryption module, we directly use enough.


import (
	"fmt"
	"encoding/base64"
) func main() { // 声明一个字符串,并转换为byte数组 input := []byte("hello world") // base64编码 encodeString := base64.StdEncoding.EncodeToString(input) fmt.Println(encodeString) // 对上面的编码结果进行base64解码 decodeBytes, err := base64.StdEncoding.DecodeString(encodeString) if err != nil { fmt.Println(err) } fmt.Println(string(decodeBytes)) } 

In this piece of data encryption and decryption is done Go language quite good.

MD5

MD5 and BASE64 almost directly to see an example of it:


import (
	"fmt"
	"crypto/md5"
) func main() { // 声明一个字符串,并转换为byte数组 input := []byte("hello world") has := md5.Sum(input) md5str1 := fmt.Sprintf("%x", has) //将[]byte转成16进制 fmt.Println(md5str1) } 

String relevant content about so much.



Guess you like

Origin www.cnblogs.com/zhangzhiping35/p/11059483.html