发表更新1 分钟读完 (大约120个字)
初始Golang
基本语法
main.go
1 2 3 4 5 6 7 8 9 10
| package main
import( "fmt" )
func main(){ fmt.Println("Hello World") }
|
Template包
Template.go1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import ( "fmt" "text/template" "os" "regexp" )
func main(){ fmt.Println("Hello Go !") tdata := []info{ {"abcabcabcfsdfsdfsdf"}, {"aaafsdgdfhergdfbdfgbdhdfgdg"}, } replaceFuncMap := template.FuncMap{ "replaceAll":replaceAll, } tmpl, err := template.New("test").Funcs(replaceFuncMap).Parse(` {{ range . }} // {{ .Info }} {{ replaceAll "(aaaa)" "1111" .Info | replaceAll "(abc)" "【$1】" | replaceAll "(aaa)" "【$1】" }} {{end}}`) if err != nil { panic(err) } err = tmpl.Execute(os.Stdout, tdata) if err != nil { panic(err) }
var a string
fmt.Scanln(&a) fmt.Print("input", a) }
func replaceAll(from ,to, input string) string{ m1 := regexp.MustCompile(from) return m1.ReplaceAllString(input,to) }
type info struct{ Info string }
|
http 库