go snippets
go实用代码片段
zsl10
这个作者很懒,什么都没留下…
展开
-
写文件
代码package mainimport ( "io" "log" "os")func main() { err := WriteToFile("result.txt", "Hello World\n") if err != nil { log.Fatal(err) }}func WriteToFile(filename string, data string) ...原创 2019-12-09 18:10:08 · 435 阅读 · 0 评论 -
go命令行传参
代码package mainimport ( "flag" "fmt")// Param 1: flag名称// Param 2: 默认值// Param 3: 提示信息// 返回值是指针类型var toRunTaskA = flag.Bool("taska", false, "Whether to run task A or taskB")func main() {...原创 2019-12-09 18:03:25 · 606 阅读 · 0 评论 -
go io.ReadCloser类型转换为string
package mainimport ( "bytes" "fmt" "net/http")func main() { response, _ := http.Get("https://www.baidu.com/") // response.Body类型为io.ReadCloser //fmt.Printf(response.Body) buf := new(bytes...原创 2019-12-05 16:01:39 · 5884 阅读 · 0 评论 -
go获取post请求参数
package mainimport ( "fmt" "io/ioutil" "log" "net/http")func main() { http.HandleFunc("/", ExampleHandler) if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err) }}fu...原创 2019-12-05 15:20:10 · 6020 阅读 · 0 评论 -
go获取get请求参数
代码package mainimport ( "encoding/json" "fmt" "log" "net/http")func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil)}func handler(w http.ResponseWriter, r *http.R...原创 2019-12-05 11:44:06 · 5493 阅读 · 0 评论 -
go过滤非数字字母字符
package mainimport ( "fmt" "log" "regexp")func main() { example := "#test!$!" reg, err := regexp.Compile("[^a-zA-Z0-9]+") if err != nil { log.Fatal(err) } processedString := reg.Replac...原创 2019-12-05 11:07:03 · 3887 阅读 · 0 评论 -
go sleep
package mainimport ( "fmt" "time")func main() { fmt.Printf("Current Unix Time: %v\n", time.Now().Unix()) time.Sleep(2 * time.Second) fmt.Printf("Current Unix Time: %v\n", time.Now().Unix()...原创 2019-12-05 11:01:52 · 464 阅读 · 0 评论 -
go操作环境变量
package mainimport ( "fmt" "os")func main() { fmt.Println(os.Getenv("HOME")) os.Setenv("Site", "test") fmt.Println(os.Getenv("Site")) fmt.Println(os.Getenv("missing"))}原创 2019-12-05 10:34:47 · 228 阅读 · 0 评论