Add embed

This commit is contained in:
Sangeeth Sudheer 2024-05-04 14:37:11 +05:30
parent 984e3daafc
commit 369fda6adb
Signed by: x
GPG Key ID: F6D06ECE734C57D1
5 changed files with 54 additions and 2 deletions

1
embed/assets/index.css Normal file
View File

@ -0,0 +1 @@
* { color: red; }

1
embed/assets/index.js Normal file
View File

@ -0,0 +1 @@
console.log("hello, world!")

37
embed/embed.go Normal file
View 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
View 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>

View File

@ -39,7 +39,8 @@ package main
// import "git.sangeeth.dev/gobyexample/exit" // import "git.sangeeth.dev/gobyexample/exit"
// import "git.sangeeth.dev/gobyexample/filepath" // import "git.sangeeth.dev/gobyexample/filepath"
// import "git.sangeeth.dev/gobyexample/dir" // 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() { func main() {
// runes.Runes() // runes.Runes()
@ -81,5 +82,6 @@ func main() {
// exit.Exit() // exit.Exit()
// filepath.Filepath() // filepath.Filepath()
// dir.Dir() // dir.Dir()
tmp.Tmp() // tmp.Tmp()
embed.Embed()
} }