Add tmp
This commit is contained in:
parent
5accc42e13
commit
984e3daafc
6
main.go
6
main.go
@ -38,7 +38,8 @@ package main
|
||||
// import "git.sangeeth.dev/gobyexample/linefilter"
|
||||
// import "git.sangeeth.dev/gobyexample/exit"
|
||||
// import "git.sangeeth.dev/gobyexample/filepath"
|
||||
import "git.sangeeth.dev/gobyexample/dir"
|
||||
// import "git.sangeeth.dev/gobyexample/dir"
|
||||
import "git.sangeeth.dev/gobyexample/tmp"
|
||||
|
||||
func main() {
|
||||
// runes.Runes()
|
||||
@ -79,5 +80,6 @@ func main() {
|
||||
// linefilter.LineFilter()
|
||||
// exit.Exit()
|
||||
// filepath.Filepath()
|
||||
dir.Dir()
|
||||
// dir.Dir()
|
||||
tmp.Tmp()
|
||||
}
|
||||
|
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