Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aee15c5

Browse files
committed
Initial commit
0 parents  commit aee15c5

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

basic_functions.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package basic_functions
2+
3+
import (
4+
"io/fs"
5+
"log"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
// version 1.0.1
12+
13+
func Read_file(file string) ([]byte, error) {
14+
if _, err := os.Stat(file); err == nil {
15+
if err != nil {
16+
return []byte(""), err
17+
}
18+
}
19+
return os.ReadFile(file)
20+
}
21+
22+
func Write_file(file string, data string) error {
23+
// save the file to disk
24+
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE, 0666)
25+
if err != nil {
26+
return err
27+
}
28+
defer f.Close()
29+
30+
_, err2 := f.WriteString(data)
31+
if err2 != nil {
32+
return err
33+
}
34+
return nil
35+
}
36+
37+
func Delete_file(file string) error {
38+
err := os.Remove(file)
39+
if err != nil {
40+
return err
41+
}
42+
return nil
43+
}
44+
45+
func File_or_directory_exists(path string) bool {
46+
_, err := os.Stat(path)
47+
if err != nil {
48+
if os.IsNotExist(err) {
49+
// File or directory does not exist
50+
return false
51+
} else {
52+
// Some other error. The file may or may not exist
53+
return false
54+
}
55+
}
56+
return true
57+
}
58+
59+
func List_files_in_dir(dir string) ([]fs.DirEntry, error) {
60+
return os.ReadDir(dir)
61+
}
62+
63+
func List_files_by_extension(dir string, extension string) []string {
64+
var extfiles []string
65+
66+
var dirfiles, err = List_files_in_dir(dir)
67+
if err != nil {
68+
log.Fatalln("List_files_by_extension - Error: " + err.Error())
69+
}
70+
71+
for _, file := range dirfiles {
72+
if strings.HasSuffix(file.Name(), extension) {
73+
extfiles = append(extfiles, file.Name())
74+
}
75+
}
76+
return extfiles
77+
}
78+
79+
func Current_directory() string {
80+
ex, err := os.Executable()
81+
if err != nil {
82+
panic(err)
83+
}
84+
85+
// the executable directory
86+
directory := filepath.Dir(ex)
87+
return directory
88+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/Papyxulo/Go_basic_functions
2+
3+
go 1.19

go.sum

Whitespace-only changes.

0 commit comments

Comments
 (0)