Compare commits
5 Commits
89d977af8f
...
5accc42e13
Author | SHA1 | Date | |
---|---|---|---|
5accc42e13 | |||
fc2dc32323 | |||
5d73c57919 | |||
8625fc2d5e | |||
ff58a428ff |
66
dir/dir.go
Normal file
66
dir/dir.go
Normal file
@ -0,0 +1,66 @@
|
||||
package dir
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func createEmptyFile(p string) {
|
||||
check(os.WriteFile(p, []byte(""), 0700))
|
||||
}
|
||||
|
||||
func Dir() {
|
||||
dirPath := "/tmp/gobyex-tempdir"
|
||||
err := os.Mkdir(dirPath, 0700)
|
||||
check(err)
|
||||
defer os.RemoveAll(dirPath)
|
||||
|
||||
createEmptyFile(filepath.Join(dirPath, "file1"))
|
||||
|
||||
check(os.MkdirAll(filepath.Join(dirPath, "child", "grandchild"), 0700))
|
||||
|
||||
createEmptyFile(filepath.Join(dirPath, "child", "grandchild", "file1"))
|
||||
createEmptyFile(filepath.Join(dirPath, "child", "grandchild", "file2"))
|
||||
createEmptyFile(filepath.Join(dirPath, "child", "grandchild", "file3"))
|
||||
|
||||
check(os.Mkdir(filepath.Join(dirPath, "child", "grandchild", "dir1"), 0700))
|
||||
|
||||
entries, err := os.ReadDir(filepath.Join(dirPath, "child", "grandchild"))
|
||||
check(err)
|
||||
|
||||
fmt.Println("Lisitng", filepath.Join(dirPath, "child", "grandchild"))
|
||||
for _, entry := range entries {
|
||||
fmt.Printf("%s, is dir? %t\n", entry.Name(), entry.IsDir())
|
||||
}
|
||||
|
||||
check(os.Chdir(dirPath))
|
||||
|
||||
entries, err = os.ReadDir(".")
|
||||
check(err)
|
||||
|
||||
fmt.Println("Listing", dirPath)
|
||||
for _, entry := range entries {
|
||||
fmt.Printf("%s, is dir? %t\n", entry.Name(), entry.IsDir())
|
||||
}
|
||||
|
||||
fmt.Println("Walking the file tree at", dirPath)
|
||||
// WalkDir is more efficient than Walk
|
||||
check(filepath.WalkDir(".", walk))
|
||||
}
|
||||
|
||||
func walk(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(" ", p, "is dir?", d.IsDir())
|
||||
return nil
|
||||
}
|
12
exit/exit.go
Normal file
12
exit/exit.go
Normal file
@ -0,0 +1,12 @@
|
||||
package exit
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Exit() {
|
||||
defer fmt.Println("this will never get printed when using os.Exit()")
|
||||
|
||||
os.Exit(3)
|
||||
}
|
26
filepath/filepath.go
Normal file
26
filepath/filepath.go
Normal file
@ -0,0 +1,26 @@
|
||||
package filepath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Filepath() {
|
||||
p := filepath.Join("a", "b", "c", "d")
|
||||
fmt.Println(p)
|
||||
|
||||
ext := filepath.Ext("main.go")
|
||||
fmt.Printf("Extension is %s\n", ext)
|
||||
fmt.Printf("Filename without ext is %s\n", strings.TrimSuffix("main.go", ext))
|
||||
|
||||
fmt.Printf("Base name is %s\n", filepath.Base("/a/b/c/file.go"))
|
||||
fmt.Printf("Dirname is %s\n", filepath.Dir("/a/b/c/file.go"))
|
||||
|
||||
relPath, _ := filepath.Rel("/a/b", "/a/c/d/f")
|
||||
fmt.Println("Rel path is", relPath)
|
||||
|
||||
fmt.Println("Is local?", filepath.IsLocal("filepath.go"))
|
||||
fmt.Println("Is abs?", filepath.IsAbs("/a/b/c/d"))
|
||||
fmt.Println("Is abs?", filepath.IsAbs("/a/b/c/d/.."))
|
||||
}
|
83
files/files.go
Normal file
83
files/files.go
Normal file
@ -0,0 +1,83 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Files() {
|
||||
Reading()
|
||||
Writing()
|
||||
}
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Reading() {
|
||||
data, err := os.ReadFile("/etc/hosts")
|
||||
check(err)
|
||||
|
||||
fmt.Println("Contents:")
|
||||
fmt.Println(string(data))
|
||||
|
||||
f, err := os.Open("/etc/hosts")
|
||||
defer f.Close()
|
||||
check(err)
|
||||
|
||||
bytes1 := make([]byte, 5)
|
||||
n1, err := f.Read(bytes1)
|
||||
check(err)
|
||||
|
||||
fmt.Printf("Read %d bytes: %v\n", n1, string(bytes1[:n1]))
|
||||
|
||||
o1, err := f.Seek(5, 0)
|
||||
check(err)
|
||||
bytes2 := make([]byte, 4)
|
||||
n2, err := f.Read(bytes2)
|
||||
check(err)
|
||||
fmt.Printf("Read %d bytes @ %d: %v\n", n2, o1, string(bytes2[:n2]))
|
||||
|
||||
o2, err := f.Seek(5, 0)
|
||||
check(err)
|
||||
bytes3 := make([]byte, 4)
|
||||
n3, err := io.ReadAtLeast(f, bytes3, 2)
|
||||
check(err)
|
||||
fmt.Printf("Read %d bytes @ %d: %v\n", n3, o2, string(bytes3))
|
||||
|
||||
o3, err := f.Seek(5, 0)
|
||||
check(err)
|
||||
br := bufio.NewReader(f)
|
||||
bytes4, err := br.Peek(4)
|
||||
check(err)
|
||||
fmt.Printf("Read @ %d: %v\n", o3, string(bytes4))
|
||||
}
|
||||
|
||||
func Writing() {
|
||||
err := os.WriteFile("/tmp/go.files.write.txt", []byte("hello go!"), 0600)
|
||||
check(err)
|
||||
|
||||
f, err := os.Create("/tmp/go.files.write2.txt")
|
||||
check(err)
|
||||
|
||||
n1, err := f.Write([]byte{87, 73, 78})
|
||||
check(err)
|
||||
fmt.Printf("Wrote %d bytes\n", n1)
|
||||
|
||||
n2, err := f.WriteString(" macOS")
|
||||
check(err)
|
||||
fmt.Printf("Wrote %d bytes\n", n2)
|
||||
|
||||
f.Sync()
|
||||
|
||||
bw := bufio.NewWriter(f)
|
||||
n3, err := bw.WriteString(" linux")
|
||||
check(err)
|
||||
fmt.Printf("Wrote %d bytes\n", n3)
|
||||
|
||||
bw.Flush()
|
||||
}
|
21
linefilter/linefilter.go
Normal file
21
linefilter/linefilter.go
Normal file
@ -0,0 +1,21 @@
|
||||
package linefilter
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func LineFilter() {
|
||||
sc := bufio.NewScanner(os.Stdin)
|
||||
|
||||
for sc.Scan() {
|
||||
cap := strings.ToUpper(sc.Text())
|
||||
fmt.Println(cap)
|
||||
}
|
||||
|
||||
if err := sc.Err(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
14
main.go
14
main.go
@ -33,7 +33,12 @@ package main
|
||||
// import "git.sangeeth.dev/gobyexample/numberparsing"
|
||||
// import "git.sangeeth.dev/gobyexample/url"
|
||||
// import "git.sangeeth.dev/gobyexample/sha"
|
||||
import "git.sangeeth.dev/gobyexample/base64"
|
||||
// import "git.sangeeth.dev/gobyexample/base64"
|
||||
// import "git.sangeeth.dev/gobyexample/files"
|
||||
// import "git.sangeeth.dev/gobyexample/linefilter"
|
||||
// import "git.sangeeth.dev/gobyexample/exit"
|
||||
// import "git.sangeeth.dev/gobyexample/filepath"
|
||||
import "git.sangeeth.dev/gobyexample/dir"
|
||||
|
||||
func main() {
|
||||
// runes.Runes()
|
||||
@ -69,5 +74,10 @@ func main() {
|
||||
// numberparsing.NumberParsing()
|
||||
// url.Url()
|
||||
// sha.Sha()
|
||||
base64.Base64()
|
||||
// base64.Base64()
|
||||
// files.Files()
|
||||
// linefilter.LineFilter()
|
||||
// exit.Exit()
|
||||
// filepath.Filepath()
|
||||
dir.Dir()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user