strings String Functions
Contains(s, substr string) bool
S string contains the string substr, returns true comprisingSplit(s, sep string) []string
S is partitioned into a string delimiter sep according sliceJoin(a []string, sep string) string
String concatenation, since the slice a link sep byTrim(s string, cutset string) string
Cutset string specified in removing the head and tail of the string sReplace(s, old, new string, n int) string
S in the string, the new string to replace the old string, n-represents the number of replacements is less than 0 represents the total replacement, the replacement is not equal to 0Repeat(s string, count int) string
S repeated string count times, and finally returns duplicate stringsIndex(s, substr string) int
Find the location in the string s where sep return position value can not be found or -1. Note that the return of 0 indicates the first position
strconv string conversion
Append family of functions
After converting to integer strings and the like, is added to an existing byte array.
package main
import (
"fmt"
"strconv"
)
func main() {
str := make([]byte, 0, 10)
str = strconv.AppendInt(str, -1, 10)
str = strconv.AppendUint(str, 1, 10) //无符号
fmt.Println(string(str))
str = strconv.AppendFloat(str, 3.14159, 'f', 2, 32)
fmt.Println(string(str))
str = strconv.AppendFloat(str, 30.14159, 'e', 3, 64)
fmt.Println(string(str))
str = strconv.AppendBool(str, true)
fmt.Println(string(str))
str = strconv.AppendQuote(str, "hello")
fmt.Println(string(str))
str = strconv.AppendQuoteRune(str, 97) //字符a对应的ascii码
fmt.Println(string(str))
}
Output:
-11
-113.14
-113.143.014e+01
-113.143.014e+01true
-113.143.014e+01true"hello"
-113.143.014e+01true"hello"'a'
Note:
1, strconv.AppendInt(dst []byte, i int64, base int)
the third parameter is hexadecimal, written here on behalf of 10 decimal.
2, strconv.AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int)
in fmt
the format labeled (b, e, E, f , g, G); prec
for the precision (length of the digital portion, not including the index portion); bitSize
specified floating-type (32: float32,64: float64).
Formatting tags:
// 'b' (-ddddp±ddd,二进制指数)
// 'e' (-d.dddde±dd,十进制指数)
// 'E' (-d.ddddE±dd,十进制指数)
// 'f' (-ddd.dddd,没有指数)
// 'g' ('e':大指数,'f':其它情况)
// 'G' ('E':大指数,'f':其它情况)
// 如果格式标记为 'e','E'和'f',则 prec 表示小数点后的数字位数
// 如果格式标记为 'g','G',则 prec 表示总的数字位数(整数部分+小数部分)
3, strconv.AppendQuote(dst []byte, s string) []byte
converts the string s to "double quotes" caused by the string, and the result is added to the end of dst, after additional return []byte
. One of the special characters will be converted to 'escape character. "
Format function Series
The other type is converted into a string.
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.FormatBool(true))
fmt.Println(strconv.FormatInt(1, 10))
fmt.Println(strconv.FormatUint(1, 10))
fmt.Println(strconv.FormatFloat(3.14159, 'f', 2, 32))
}
Output:
true
1
1
3.14
Parse function Series
A string to an int type uint bool float, err specify whether the conversion was successful.
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.ParseBool("true"))
fmt.Println(strconv.ParseInt("100", 10, 0))
fmt.Println(strconv.Atoi("100")) // 通常使用这个函数,而不使用 ParseInt
fmt.Println(strconv.ParseUint("100", 10, 0))
fmt.Println(strconv.ParseFloat("3.14159", 32))
}
Output:
true <nil>
100 <nil>
100 <nil>
100 <nil>
3.141590118408203 <nil>
Note:
1, strconv.ParseInt(s string, base int, bitSize int)
the third argument bitSize
is the return result of bit size, which is int8 int16 int32 int64. If to 0, the default value strconv.IntSize
, the machine size is 64 64.
Quote Series Function
1, strconv.Quote(s string) string
the string s to the character string "double quotes" caused, wherein the special characters will be converted to "escape character", "character can not be displayed" will be converted to "escape characters."
2, strconv.QuoteToASCII(s string) string
converts the string to s '' due to the ASCII string "non-ASCII characters," and "special characters" is converted to "escape characters."
3, strconv.QuoteRune(r rune) string
Unicode characters are converted to "single quotes" delimited strings, "special characters" is converted to "escape characters."
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.Quote(`hello go 语言\n`))
fmt.Println(strconv.Quote("hello go 语言\n"))
fmt.Println(strconv.QuoteToASCII(`hello go 语言\n`))
fmt.Println(strconv.QuoteRune(97))
fmt.Println(strconv.QuoteRuneToASCII(97))
}
Output:
"hello go 语言\\n"
"hello go 语言\n"
"hello go \u8bed\u8a00\\n"
'a'
'a'
encoding
encoding/json
json.Marshal(v interface{}) ([]byte, error)
Generating JSONjson.Unmarshal(data []byte, v interface{}) error
To parse JSON interface
JSON parsing
package main
import (
"encoding/json"
"fmt"
)
type Employee struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
type EmployeeSlice struct {
Employees []Employee `json:"employees"`
}
func main() {
str := `{"employees":[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"}]}`
var res EmployeeSlice
json.Unmarshal([]byte(str), &res)
fmt.Println(res)
fmt.Println(res.Employees[0].FirstName)
}
Output:
[{Bill Gates} {George Bush}]}
Bill
Generating JSON:
package main
import (
"encoding/json"
"fmt"
)
type Employee struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
type EmployeeSlice struct {
Employees []Employee `json:"employees"`
}
func main() {
data := EmployeeSlice{[]Employee{
{FirstName:"Bill", LastName:"Gates"},
{FirstName:"George", LastName:"Bush"},
}}
res,_ := json.Marshal(data)
fmt.Println(string(res))
}
Output:
{"employees":[{"firstName":"Bill","lastName":"Gates"},{"firstName":"George","lastName":"Bush"}]}
crypto
md5
package main
import (
"crypto/md5"
//"io"
"fmt"
"encoding/hex"
)
func main(){
h := md5.New()
//io.WriteString(h, "123456")
h.Write([]byte("123456"))
cipherStr := h.Sum(nil)
fmt.Println(cipherStr) //一个128bit的16字节byte数组
fmt.Println(hex.EncodeToString(cipherStr)) // 输出加密结果
}
Run output:
[225 10 220 57 73 186 89 171 190 86 224 87 242 15 136 62]
e10adc3949ba59abbe56e057f20f883e
Using io.WriteString(h, "123456")
the h.Write([]byte("123456"))
same effect, multiple times Write
, once at the end of the string will be appended to the front.
In addition md5
, there are sha1
, sha256
to use the method is similar:
//import "crypto/sha256"
h := sha256.New()
io.WriteString(h, "123456")
fmt.Printf("%x", h.Sum(nil))
//import "crypto/sha1"
h := sha1.New()
io.WriteString(h, "123456")
fmt.Printf("%x", h.Sum(nil))
base64
package main
import (
"encoding/base64"
"fmt"
)
// 编码
func base64Encode(str []byte) []byte {
return []byte(base64.StdEncoding.EncodeToString(str))
}
// 解码
func base64Decode(str []byte) ([]byte, error){
return base64.StdEncoding.DecodeString(string(str))
}
func main(){
str := "hello"
enc_str := base64Encode([]byte(str))
fmt.Println(enc_str)
fmt.Println(string(enc_str))
dec_str,err := base64Decode(enc_str)
if(err != nil){
fmt.Println(err.Error())
}
fmt.Println(dec_str)
fmt.Println(string(dec_str))
}
Output:
[97 71 86 115 98 71 56 61]
aGVsbG8=
[104 101 108 108 111]
hello
AES
net
net/url
net/http
net/http
It has good support for the common GET, POST request.
get request
package main
import (
"net/http"
"fmt"
"io/ioutil"
"io"
)
func main() {
var url string = "http://httpbin.org/get?page=1&limit=2"
resp, err := http.Get(url)
if (err != nil) {
fmt.Println(err.Error())
}
fmt.Println(resp.Status) //200 ok
fmt.Println(resp.StatusCode) //200
var bodyReader io.ReadCloser = resp.Body //返回的是io.Reader
body, _ := ioutil.ReadAll(bodyReader)
fmt.Println(string(body))
}
Output:
200 OK
200
{
"args": {
"limit": "2",
"page": "1"
},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
"Host": "httpbin.org",
"User-Agent": "Go-http-client/1.1"
},
"origin": "221.217.54.202",
"url": "http://httpbin.org/get?page=1&limit=2"
}
post form request
post request to use form http.PostForm()
. In addition to the need for additional parameters, as other and get request.
package main
import (
"net/http"
"fmt"
"io/ioutil"
"io"
"net/url"
)
func main() {
var apiURL string = "http://httpbin.org/post?page=1"
var params url.Values = url.Values{"names": []string{"yjc", "yjc1"}}
params.Set("age", "20")
resp, err := http.PostForm(apiURL, params)
if (err != nil) {
fmt.Println(err.Error())
}
fmt.Println(resp.Status) //200 ok
fmt.Println(resp.StatusCode) //200
var bodyReader io.ReadCloser = resp.Body //返回的是io.Reader
body, _ := ioutil.ReadAll(bodyReader)
fmt.Println(string(body))
}
Output:
200 OK
200
{
"args": {
"page": "1"
},
"data": "",
"files": {},
"form": {
"age": "20",
"names": [
"yjc",
"yjc1"
]
},
"headers": {
"Accept-Encoding": "gzip",
"Connection": "close",
"Content-Length": "27",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "Go-http-client/1.1"
},
"json": null,
"origin": "221.217.54.202",
"url": "http://httpbin.org/post?page=1"
}
Package
package tool
import (
"io/ioutil"
"net/http"
"net/url"
"fmt"
)
// get 网络请求
func Get(apiURL string, params url.Values) (rs []byte, err error) {
var Url *url.URL
Url, err = url.Parse(apiURL)
if err != nil {
fmt.Printf("解析url错误:\r\n%v", err)
return nil, err
}
//如果参数中有中文参数,这个方法会进行URLEncode
Url.RawQuery = params.Encode()
resp, err := http.Get(Url.String())
if err != nil {
fmt.Println("err:", err)
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
// post 网络请求 ,params 是url.Values类型
func Post(apiURL string, params url.Values) (rs []byte, err error) {
resp, err := http.PostForm(apiURL, params)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
net/http/pprof
The package do performance monitoring code. Example of use:
package main
import (
"net/http"
_ "net/http/pprof"
)
func main(){
//提供给负载均衡探活以及pprof调试
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})
http.ListenAndServe(":10000", nil)
}
After running, open the browser http://127.0.0.1:10000/debug/pprof/
will be able to see some of the monitoring information. According to the above production environment generally do not write so are generally open coroutine:
go http.ListenAndServe(":10000", nil)
How to Start PProf visual interface?
You need
graphviz
support, to http://www.graphviz.org/download/ download, and the bin to the environment variable. Mac can be installed using the brew .
Below profile
(CPU profile) as an example:
method one:
go tool pprof -http=:8080 http://localhost:10000/debug/pprof/profile
Method Two:
go tool pprof http://localhost:10000/debug/pprof/profile
Then in the interactive command line web
to jump to the default browser. (The machine test failed, opened the Sublime text)
Reference: Golang pprof Detailed
https://studygolang.com/articles/14519
the
Most of the functions in os file operations are inside the package.
Directory operations:
func Mkdir(name string, perm FileMode) error
Create a name for the directory name, the permission settings are perm, such as 0777.func MkdirAll(path string, perm FileMode) error
Create multilevel subdirectories according path.func Remove(name string) error
Delete the name of the directory name, and when there is a file directory or other directory to be wrong.func RemoveAll(path string) error
According to path delete levels of subdirectories, if the path is a single name, then the subdirectory under the directory deleted.
Example:
package main
import (
"os"
"fmt"
)
func main() {
os.Mkdir("tmp", 0755)
os.MkdirAll("tmp/test/test2", 0755)
err := os.Remove("tmp")
if err != nil{
fmt.Println(err)
}
os.RemoveAll("tmp")
}
Run output:
remove tmp: The directory is not empty.
File operations:
func Create(name string) (file *File, err Error)
Creating the file name provided by the new file and returns a file object, the default permissions are 0666 files, the returned object is read-write.func NewFile(fd uintptr, name string) *File
Create the appropriate file based on file descriptor returns a file object.func Open(name string) (file *File, err Error)
This method opens a name for the name of the file, but is read-only, in fact, achieve internal calls
OpenFile.func OpenFile(name string, flag int, perm uint32) (file *File, err Error)
Open the file name as the name, flag is on the way, read-only, read and write, perm permissionsfunc (file *File) Write(b []byte) (n int, err Error)
Write byte type of information to a filefunc (file *File) WriteAt(b []byte, off int64) (n int, err Error)
Start writing a byte of information in the specified locationfunc (file *File) WriteString(s string) (ret int, err Error)
Write string to file informationfunc (file *File) Read(b []byte) (n int, err Error)
Read data into bfunc (file *File) ReadAt(b []byte, off int64) (n int, err Error)
Reads data from off into bfunc Remove(name string) Error
Call this function you can delete the file named name. Delete files and delete folders are the same function.
I / ioutil
ioutil some practical package encapsulates I / O function, to provide a total of a variable external use, method 7.
func ReadFile(filename string) ([]byte, error)
ReadFile read all the data in the file, and returns the contents read errors encountered.func WriteFile(filename string, data []byte, perm os.FileMode) error
WriteFile write data to a file, if the original data before it will be empty, if the file does not exist will be specified permission to create the file.func ReadDir(dirname string) ([]os.FileInfo, error)
ReadDir read all directories and files in the specified directory (not including subdirectories). Returns the list of files and information read errors encountered, the list is sorted.func TempFile(dir, prefix string) (f *os.File, err error)
TempFile create a temporary file prefix prefix in dir directory and open it in read-write mode. Create a file object and returns an error message encountered. If dir, then create a file (see empty in the default temporary directoryos.TimeDir
), multiple calls to create different temporary files, the caller can get the full path of the file by f.Name (). Temporary files created by calling this function, should be deleted by the caller himself.func TempDir(dir, prefix string) (name string, err error)
TempDir function is to create a temporary directory (and other functions, like TempFile), complete directory created and returned error messages encountered.func ReadAll(r io.Reader) ([]byte, error)
All data ReadFile to read the file, the data is read and return errors encountered. If the reading is successful, err returns nil, instead of EOF. This method enablesio.Reader
to use interface.func NopCloser(r io.Reader) io.ReadCloser
NopCloser will be packaged as a ReadCloser r type, but the Close method does nothing.
I
io package is to provide basic primitive IO interface (I / O primitives).
golang of io package - to Liu's personal space - Open Source China
https://my.oschina.net/liudiwu/blog/305023
flag
Used to get command line arguments.
Example:
package main
import (
"flag"
"fmt"
)
func main() {
backup_dir := flag.String("b", "/home/default_dir", "backup path")
debug_mode := flag.Bool("d", false, "debug mode")
flag.Parse()
fmt.Println("backup_dir: ", *backup_dir)
fmt.Println("debug_mode: ", *debug_mode)
}
golang of flag.String - blog Park - hezhixiong
http://www.cnblogs.com/hezhixiong/p/4659477.html
sync
sync.WaitGroup
package main
import (
"fmt"
"time"
)
func main(){
for i := 0; i < 100 ; i++{
go fmt.Println(i)
}
time.Sleep(time.Second)
}
Above the main thread to wait for the goroutine
run is completed, the program had to end use time.Sleep()
to sleep for some time, waiting for other threads fully operational. But most of the time we can not predict for
the length of the inner loop code running time. You can not use this time time.Sleep()
to wait for the completion of the operation.
In this case, go in a language other tools sync.WaitGroup
can be more convenient to help us achieve this goal. WaitGroup
Internal object has a counter, initially starting from 0, which has three methods: Add()
, Done()
, Wait()
it is used to control the number of counters. Add(n)
The counter is set to n
, Done()
each time the counter -1
, wait()
will be blocked from running code until the counter value to decrease 0
.
WaitGroup using the above code can be modified to:
func main() {
wg := sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
go func(i int) {
fmt.Println(i)
wg.Done()
}(i)
}
wg.Wait()
}
See: Golang sync.WaitGroup usage
https://studygolang.com/articles/12972?fr=sidebar
runtime
runtime/debug
For:
- Mandatory garbage collection
- Set a target percentage of garbage collection
- Setting the maximum memory is a single go coroutine call stack can be used
- Maximum operating system setup program can go a number of threads
- Run the setup program request is only triggered panic, but not collapse
- Write stats collection of information waste
- Heap memory allocation and will be described in which the object is written to a file
- Get go coroutine call stack trace
- The stack trace to the standard error
详见:go-runtime/debug
https://www.jianshu.com/p/0b3d11f7af57