golangのtips
配列から要素を削除
func (s *SliceSample) Remove(index int) { res := []string{} for i, v := range s.List { if i == index { continue } res = append(res, v) } s.List = res } 正規表現
package main import "fmt" import "regexp" func main() { str := "Copyright 2015 by ASHITANI Tatsuji." rep := regexp.MustCompile(`[A-Za-z]*right`) str = rep.ReplaceAllString(str, "Copyleft") fmt.Println(str) // => "Copyleft 2015 by ASHITANI Tatsuji." } メソッドの継承
テンプレートをstringで取得
var tpl bytes.Buffer if err := t.Execute(&tpl, data); err != nil { return err } result := tpl.String() Simply output go html template execution to strings (Example)
uniqしたい
arr := [string]{"a", "b", "c", "a"} m := make(map[string]bool) uniq := [] string{} for _, ele := range arr { if !m[ele] { m[ele] = true uniq = append(uniq, ele) } } fmt.Printf("%v", uniq) // ["a", "b", "c"] Goのsliceで重複を削除する - emahiro/b.log
build方法
for mac
$ ls main.go $ GOOS=darwin GOARCH=amd64 go build -o ./test_exe_for_mac gccのエラーが発生する
exec: "gcc": executable file not found in %PATH% のエラーが発生する CGO_ENABLED=0 で実行する。
GO1.11.9のコンテナでgo testしようとするとexec: “gcc”: executable file not found in $PATH と怒られる - Qiita
This post is licensed under CC BY 4.0 by the author.