10 examples of 'golang read file' in Go

Every line of 'golang read file' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
10func main() {
11 input := util.ReadFile("../input.txt")
12
13 lines := strings.Split(input, "\n")
14
15 grid := make([][]string, len(lines))
16 for i, v := range lines {
17 grid[i] = strings.Split(v, "")
18 }
19
20 // can map biodiversity scores because they're essentially bitmaps
21 previousBiodiversities := map[int]bool{getBiodiversity(grid): true}
22
23 // run indefinitely
24 for {
25 // step through a minute
26 grid = stepMinute(grid)
27 newBiodiversity := getBiodiversity(grid)
28
29 // if new biodiversity score is already in the map, print it and exit
30 if previousBiodiversities[newBiodiversity] {
31 fmt.Println("Repeated biodiversity score", newBiodiversity)
32 return
33 }
34
35 // set biodiversity score into in map
36 previousBiodiversities[newBiodiversity] = true
37 }
38}
192func (f Files) FileRead(path string) ([]byte, error) {
193 return ioutil.ReadFile(path)
194}
46func (s *FileSystemImageSource) read(file string) ([]byte, error) {
47 buf, err := ioutil.ReadFile(file)
48 if err != nil {
49 return nil, ErrInvalidFilePath
50 }
51 return buf, nil
52}
10func main() {
11 // need to read the input.txt file and split each line into a slice
12 input := util.ReadFile("../input.txt")
13 stringSlice := strings.Split(input, "\n")
14
15 // split into a 2D grid with each character
16 gridSlice := make([][]string, len(stringSlice))
17 for i, str := range stringSlice {
18 gridSlice[i] = strings.Split(str, "")
19 }
20
21 // will be the final result value
22 var result int
23 var finalCoords [2]int
24 // iterate through entire slice, for each asteroid found, call a helper function
25 for rowIndex, rowSlice := range gridSlice {
26 for colIndex, element := range rowSlice {
27 if element == "#" {
28 // # are "asteroids", . are empty space
29 // helper function will return how many asteroids are "findable" from the current asteroid
30 visibleFromElement := visibleFromAsteroid(gridSlice, rowIndex, colIndex)
31
32 // take max of return of helper function at end of each loop
33 if result < visibleFromElement {
34 result = visibleFromElement
35 finalCoords[0], finalCoords[1] = rowIndex, colIndex
36 }
37 }
38 }
39 }
40
41 // print out the max found
42 fmt.Printf("best asteroid for the station: row[%v] col[%v]\n", finalCoords[0], finalCoords[1]) // [13, 11]
43 fmt.Println("from 13, 11 (y, x)", result)
44}
12func main() {
13 fmt.Printf("Uncompressed length: v1: %v v2: %v", decompressString(common.ReadEntireFile(inputPath)), calcDecompressed(common.ReadEntireFile(inputPath)))
14}
22func read() {
23 file, err := os.Open("textfile.txt")
24 if err != nil {
25 panic(err)
26 }
27 defer file.Close()
28 fmt.Println("Read file:")
29 io.Copy(os.Stdout, file)
30}
12func main() {
13 // parse input file into a slice of numbers
14 input := util.ReadFile("../input.txt")
15 // input := "03036732577212944063491565474664" // test should output "84462026"
16 characters := strings.Split(input, "")
17
18 digits := make([]int, len(characters)*10000)
19 for i := 0; i < 10000; i++ {
20 for j, v := range characters {
21 digits[i*len(characters)+j], _ = strconv.Atoi(v)
22 }
23 }
24
25 var offsetIndex int
26 for i := 0; i < 7; i++ {
27 offsetIndex *= 10
28 offsetIndex += digits[i]
29 }
30 fmt.Println("offsetIndex", offsetIndex)
31
32 // run through 100 phases, overwriting digits
33 for i := 0; i < 100; i++ {
34 digits = getNextOutputNumber(digits)
35 // output a time to make sure this is running fast enough
36 fmt.Printf("output received at %v, %v to go\n", time.Now(), 100-i-1)
37 }
38
39 // Transform into github.com/alexchao26/advent-of-code-go output
40 var firstEightDigits int
41 for i := 0; i < 8; i++ {
42 firstEightDigits *= 10
43 firstEightDigits += digits[i+offsetIndex]
44 }
45 fmt.Printf("\nOffset 8 digits after 100 phases: %v\n", firstEightDigits)
46 fmt.Println("Expect 84462026 for test, 36265589 for actual input")
47}
58func readJsonFile(path string) (jsonData []byte, err error) {
59 jsonData = nil
60 _, err = os.Stat(path)
61 if err != nil {
62 if len(path) == 0 {
63 //log.log.With Println("Need request json file")
64 } else {
65 fmt.Printf("Not Found %s \n", path)
66 }
67 return
68 }
69
70 jsonData, err = ioutil.ReadFile(path)
71 if err != nil {
72 fmt.Printf("dots_client_controller.readJsonFile -- File error: %v\n", err)
73 return
74 }
75 return
76}
66func (cache *Cache) Read(envkeyParam string) ([]byte, error) {
67 path := filepath.Join(cache.Dir, envkeyParam)
68 b, err := ioutil.ReadFile(path)
69 select {
70 case cache.Done <- err:
71 default:
72 }
73 return b, err
74}
13func readFile(filepath string) []byte {
14 // TODO only read as deep into the file as we need
15 bytes, err := ioutil.ReadFile(filepath)
16
17 if err != nil {
18 fmt.Print(err)
19 }
20
21 return bytes
22}

Related snippets