Hi 👋
I have tried to learn some go but I am still very much at the beginning, like understanding how to work with variables and functions. My background is mostly in python but I am not a programmer by trade.
package main
import (
"crypto/sha256"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
func write_lines_to_file(lines []string, output_file string) {
f, err2 := os.Create(output_file)
if err2 != nil {
log.Fatal(err2)
}
defer f.Close()
for _, line := range lines {
_, err := f.WriteString(line + "\n")
if err != nil {
log.Fatal(err2)
}
}
}
func get_size_and_hash(file_path string) (int, string) {
file, err := os.Open(file_path)
if err != nil {
panic(err)
}
defer file.Close()
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
panic(err)
}
sum := fmt.Sprintf("%x", hash.Sum(nil))
file, err2 := os.Open(file_path)
if err2 != nil {
log.Fatal(err2)
}
fi, err2 := file.Stat()
if err != nil {
log.Fatal(err2)
}
my_size := fi.Size()
return int(my_size), string(sum)
}
func get_list_of_files(target_directory string) []string {
var files []string
err := filepath.Walk(target_directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return nil
}
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
log.Fatal(err)
}
return files
}
func main() {
// accept directory as user input
target_directory := os.Args[1]
my_files := get_list_of_files(target_directory)
var content []string
for _, file := range my_files {
size, hash := get_size_and_hash(file)
var str_file string = string(file)
str_size := fmt.Sprint(size)
var str_hash string = string(hash)
// structure: file, checksum, size
combined_line := str_file + "," + str_hash + "," + str_size
content = append(content, combined_line)
}
var output_file string = target_directory + "/PULP_MANIFEST"
write_lines_to_file(content, output_file)
}
I am testing this using the following command: rm -f test_input/PULP_MANIFEST && go fmt pulp_manifest.go && go build pulp_manifest.go && ./pulp_manifest test_input && cat test_input/PULP_MANIFEST
on Fedora with go 1.20
Known Limitations
Untested
I am looking for the following feedback:
Right now, I believe my rewrite works. Feel free to shatter my assumption. Cheers.
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.
Hope you enjoy the instance!
Follow the wormhole through a path of communities !webdev@programming.dev
Did you ask GPT? Can it help with go? No idea. But it can help me with exactly those kinds of issues when I need help in python, C, and VBA.