Compare commits
2 Commits
5accc42e13
...
369fda6adb
Author | SHA1 | Date | |
---|---|---|---|
369fda6adb | |||
984e3daafc |
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>
|
8
main.go
8
main.go
@ -38,7 +38,9 @@ package main
|
|||||||
// import "git.sangeeth.dev/gobyexample/linefilter"
|
// import "git.sangeeth.dev/gobyexample/linefilter"
|
||||||
// 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/embed"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// runes.Runes()
|
// runes.Runes()
|
||||||
@ -79,5 +81,7 @@ func main() {
|
|||||||
// linefilter.LineFilter()
|
// linefilter.LineFilter()
|
||||||
// exit.Exit()
|
// exit.Exit()
|
||||||
// filepath.Filepath()
|
// filepath.Filepath()
|
||||||
dir.Dir()
|
// dir.Dir()
|
||||||
|
// tmp.Tmp()
|
||||||
|
embed.Embed()
|
||||||
}
|
}
|
||||||
|
37
tmp/tmp.go
Normal file
37
tmp/tmp.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package tmp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func check(err error) {
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Tmp() {
|
||||||
|
f, err := os.CreateTemp("", "gobyexample")
|
||||||
|
check(err)
|
||||||
|
fmt.Println("Created tmp file", f.Name())
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
|
||||||
|
n1, err := f.WriteString("test")
|
||||||
|
check(err)
|
||||||
|
fmt.Println("Wrote", n1, "bytes")
|
||||||
|
|
||||||
|
dirPath, err := os.MkdirTemp("", "gobyexample")
|
||||||
|
check(err)
|
||||||
|
fmt.Println("Created temp dir at", dirPath)
|
||||||
|
defer os.RemoveAll(dirPath)
|
||||||
|
|
||||||
|
err = os.WriteFile(filepath.Join(dirPath, "file1"), []byte("tmpdir file"), 0600)
|
||||||
|
check(err)
|
||||||
|
fmt.Println("Stuff written to tmp file inside", dirPath)
|
||||||
|
b1, err := os.ReadFile(filepath.Join(dirPath, "file1"))
|
||||||
|
check(err)
|
||||||
|
fmt.Println(strconv.Quote(string(b1)), "was written to file")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user