初始Golang

初始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.go
1
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)
// return strings.ReplaceAll(input,from ,to)
}


type info struct{
Info string
}

http 库

作者

zhang

发布于

2023-02-03

更新于

2023-09-19

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×