Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a text file? [duplicate]

Tags:

go

I'm trying to read "file.txt" and put the contents into a variable using Golang. Here is what I've tried...

package main  import (     "fmt"     "os"     "log" )  func main() {     file, err := os.Open("file.txt")     if err != nil {         log.Fatal(err)     }      fmt.Print(file) } 

The file gets read successfully and the return from os.Open returns a type of *os.File

like image 965
Ari Seyhun Avatar asked Mar 20 '16 08:03

Ari Seyhun


People also ask

How do I find duplicates in a text file?

To start your duplicate search, go to File -> Find Duplicates or click the Find Duplicates button on the main toolbar. The Find Duplicates dialog will open, as shown below. The Find Duplicates dialog is intuitive and easy to use. The first step is to specify which folders should be searched for duplicates.

How do I remove duplicates in a text file?

The uniq command is used to remove duplicate lines from a text file in Linux. By default, this command discards all but the first of adjacent repeated lines, so that no output lines are repeated. Optionally, it can instead only print duplicate lines. For uniq to work, you must first sort the output.

How do I find duplicates in a text file in Linux?

The uniq command in Linux is used to display identical lines in a text file. This command can be helpful if you want to remove duplicate words or strings from a text file. Since the uniq command matches adjacent lines for finding redundant copies, it only works with sorted text files.


1 Answers

It depends on what you are trying to do.

file, err := os.Open("file.txt") fmt.print(file) 

The reason it outputs &{0xc082016240}, is because you are printing the pointer value of a file-descriptor (*os.File), not file-content. To obtain file-content, you may READ from a file-descriptor.


To read all file content(in bytes) to memory, ioutil.ReadAll

package main  import (     "fmt"     "io/ioutil"     "os"     "log" )  func main() {     file, err := os.Open("file.txt")     if err != nil {         log.Fatal(err)     }     defer func() {         if err = file.Close(); err != nil {             log.Fatal(err)         }     }()     b, err := ioutil.ReadAll(file)   fmt.Print(b) } 

But sometimes, if the file size is big, it might be more memory-efficient to just read in chunks: buffer-size, hence you could use the implementation of io.Reader.Read from *os.File

func main() {     file, err := os.Open("file.txt")     if err != nil {         log.Fatal(err)     }     defer func() {         if err = file.Close(); err != nil {             log.Fatal(err)         }     }()       buf := make([]byte, 32*1024) // define your buffer size here.      for {         n, err := file.Read(buf)          if n > 0 {             fmt.Print(buf[:n]) // your read buffer.         }          if err == io.EOF {             break         }         if err != nil {             log.Printf("read %d bytes: %v", n, err)             break         }     }  } 

Otherwise, you could also use the standard util package: bufio, try Scanner. A Scanner reads your file in tokens: separator.

By default, scanner advances the token by newline (of course you can customise how scanner should tokenise your file, learn from here the bufio test).

package main  import (     "fmt"     "os"     "log"     "bufio" )  func main() {     file, err := os.Open("file.txt")     if err != nil {         log.Fatal(err)     }     defer func() {         if err = file.Close(); err != nil {             log.Fatal(err)         }     }()      scanner := bufio.NewScanner(file)      for scanner.Scan() {             // internally, it advances token based on sperator         fmt.Println(scanner.Text())  // token in unicode-char         fmt.Println(scanner.Bytes()) // token in bytes      } } 

Lastly, I would also like to reference you to this awesome site: go-lang file cheatsheet. It encompassed pretty much everything related to working with files in go-lang, hope you'll find it useful.

like image 87
Roy Lee Avatar answered Sep 23 '22 09:09

Roy Lee