organizing/adding some useful snippets in go folder

This commit is contained in:
alexchao26
2020-02-05 13:53:37 -05:00
parent fb6199ea22
commit fbc9d6f082
4 changed files with 88 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
<x=14, y=2, z=8>
<x=7, y=4, z=10>
<x=1, y=17, z=16>
<x=-4, y=-1, z=1>
+38
View File
@@ -0,0 +1,38 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
)
func main() {
stringSlice := readInputFile("./input.txt")
fmt.Println(stringSlice)
}
// helper function to put the input file into a slice of strings
// (each elements is a line of the txt file)
func readInputFile(path string) []string {
// var pixelString string
resultSlice := make([]string, 0)
absPath, _ := filepath.Abs(path)
file, err := os.Open(absPath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// pixelString = line
resultSlice = append(resultSlice, line)
}
// return pixelString
return resultSlice
}