Created
February 10, 2021 14:43
-
-
Save Miqueas/70965ab7ea03d85f43a379e0860d86eb to your computer and use it in GitHub Desktop.
[Go] Gets the size (in bytes) of a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// We need only this modules | |
import ( | |
"os" // For open the file | |
"fmt" // For print data | |
"io/ioutil" // For read data | |
) | |
// A little function for error checking | |
func check(e error, msg string, args ...interface{}) { | |
if e != nil { | |
fmt.Printf(msg, args...) | |
panic(e) | |
} | |
} | |
func main() { | |
// Holds the error | |
var err error | |
// Open the file, change the name for something in your system | |
file, err := os.Open("filename") | |
check(err, "Can't open file!") | |
// Read the file data and returns it as an array of type byte | |
bytes, err := ioutil.ReadAll(file) | |
check(err, "Can't read the file!") | |
// Prints the size! | |
fmt.Printf("File has %d bytes!\n", len(bytes)) | |
// And close the file | |
err = file.Close() | |
check(err, "Can't close the file!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment