Add embed
This commit is contained in:
parent
984e3daafc
commit
369fda6adb
1
embed/assets/index.css
Normal file
1
embed/assets/index.css
Normal file
@ -0,0 +1 @@
|
||||
* { color: red; }
|
1
embed/assets/index.js
Normal file
1
embed/assets/index.js
Normal file
@ -0,0 +1 @@
|
||||
console.log("hello, world!")
|
37
embed/embed.go
Normal file
37
embed/embed.go
Normal file
@ -0,0 +1,37 @@
|
||||
package embed
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
//go:embed home.html
|
||||
var HomeTemplateString string
|
||||
|
||||
//go:embed assets/index.css
|
||||
//go:embed assets/index.js
|
||||
var folder embed.FS
|
||||
|
||||
func Embed() {
|
||||
cssBytes, err := folder.ReadFile(filepath.Join("assets", "index.css"))
|
||||
check(err)
|
||||
jsBytes, err := folder.ReadFile(filepath.Join("assets", "index.js"))
|
||||
check(err)
|
||||
fmt.Println("index.css:")
|
||||
fmt.Println(string(cssBytes))
|
||||
fmt.Println("index.js:")
|
||||
fmt.Println(string(jsBytes))
|
||||
|
||||
fmt.Println("Parsing template and printing it:")
|
||||
t := template.Must(template.New("home").Parse(HomeTemplateString))
|
||||
t.Execute(os.Stdout, string("John"))
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
11
embed/home.html
Normal file
11
embed/home.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{{ . }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ . }}</h1>
|
||||
</body>
|
||||
</html>
|
6
main.go
6
main.go
@ -39,7 +39,8 @@ package main
|
||||
// import "git.sangeeth.dev/gobyexample/exit"
|
||||
// import "git.sangeeth.dev/gobyexample/filepath"
|
||||
// import "git.sangeeth.dev/gobyexample/dir"
|
||||
import "git.sangeeth.dev/gobyexample/tmp"
|
||||
// import "git.sangeeth.dev/gobyexample/tmp"
|
||||
import "git.sangeeth.dev/gobyexample/embed"
|
||||
|
||||
func main() {
|
||||
// runes.Runes()
|
||||
@ -81,5 +82,6 @@ func main() {
|
||||
// exit.Exit()
|
||||
// filepath.Filepath()
|
||||
// dir.Dir()
|
||||
tmp.Tmp()
|
||||
// tmp.Tmp()
|
||||
embed.Embed()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user