package main
import (
"bytes"
"fmt"
"log"
"github.com/PuerkitoBio/goquery"
)
func main() {
rawHtml := bytes.NewBuffer([]byte(`
<div name="lesson" class="lesson" title="
<p class=tooltip_name><b>1-1</b></p>
<p class=tooltip_professor>Преп2</p>
<hr />
<p class=tooltip_aud>Практическое занятие</p>
" style="background-color: #D5F6FF;">
<p>Физ культура</p>
<p class="lesson_aud">ЗАЛ1</p>
</div>
`))
doc, err := goquery.NewDocumentFromReader(rawHtml)
if err != nil {
log.Fatal(err)
}
title, ok := doc.Find(".lesson").Attr("title") // получаем значение title
if !ok {
log.Fatal("Cannot find the node with the class 'lesson'")
}
// Парсим то, что лежит в title
docFromTitle, err := goquery.NewDocumentFromReader(bytes.NewBuffer([]byte(title)))
if err != nil {
log.Fatal(err)
}
// Получаем элемент с именем преподавателя
teacher := docFromTitle.Find(".tooltip_professor").Text()
fmt.Println(teacher)
}
package main
import (
"net/http"
"gopkg.in/gin-gonic/gin.v1"
)
func main() {
r := gin.Default()
r.GET("/ping", PingHandler)
r.GET("/user/:name", UserGetHandler)
r.POST("/user/:name", UserPostHandler)
r.Run()
}
func PingHandler(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}
func UserGetHandler(c *gin.Context) {
name := c.Param("name")
c.String(http.StatusOK, "Hello %s\n", name)
}
func UserPostHandler(c *gin.Context) {
name := c.Param("name")
// Do some actions
c.String(http.StatusOK, "User %s was edited\n", name)
}
$ curl 127.0.0.1:8080/ping
{"message":"pong"}
$ curl 127.0.0.1:8080/user/john
Hello john
$ curl -XPOST 127.0.0.1:8080/user/john
User john was edited
package main
import (
"bytes"
"fmt"
"html/template"
"log"
)
type Data struct {
Name string
}
func main() {
var tmpl *template.Template
funcMap := template.FuncMap{
"executeIfExists": func(name string, data interface{}) template.HTML {
t := tmpl.Lookup(name)
if t == nil {
return ""
} else {
buf := bytes.NewBuffer([]byte{})
err := t.Execute(buf, data)
if err != nil {
log.Printf("Error executing template '%s': %v", name, err)
return ""
} else {
return template.HTML(buf.Bytes())
}
}
},
}
tmpl = template.Must(template.New("main").Funcs(funcMap).
Parse(`Content: {{ executeIfExists "content" . }} {{ executeIfExists "someAbsentTemplate" . }}`))
tmpl = template.Must(tmpl.New("content").Parse(`Hello, <b>{{ .Name }}</b>!`))
data := Data{
Name: "John",
}
buf := bytes.NewBuffer([]byte{})
err := tmpl.ExecuteTemplate(buf, "main", data)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(buf.Bytes()))
}
package main
import (
"bytes"
"fmt"
"html/template"
"log"
)
func main() {
out := bytes.NewBuffer([]byte{})
t, err := template.New("foo").Parse(`<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var data = JSON.parse( {{.}} );
</script>
</head>
<body>
</body>
</html>`)
if err != nil {
log.Fatal("Cannot parse the template: ", err)
}
err = t.Execute(out, `[{"ID":"1"}]`)
if err != nil {
log.Fatal("Cannot execute the template: ", err)
}
fmt.Println(string(out.Bytes()))
}
package main
import (
"bytes"
"fmt"
"html/template"
"log"
)
type A []struct {
ID string
}
func main() {
out := bytes.NewBuffer([]byte{})
t, err := template.New("foo").Parse(`<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var data = {{.}};
</script>
</head>
<body>
</body>
</html>`)
if err != nil {
log.Fatal("Cannot parse the template: ", err)
}
data := A{
{ID: "1"},
}
err = t.Execute(out, data)
if err != nil {
log.Fatal("Cannot execute the template: ", err)
}
fmt.Println(string(out.Bytes()))
}