Created
February 28, 2018 20:22
-
-
Save scriptnull/e8d2b42b364bf10a18402ba8dabb8267 to your computer and use it in GitHub Desktop.
Opens a file and prints the file descriptor
This file contains hidden or 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 | |
| import ( | |
| "fmt" | |
| "log" | |
| "os" | |
| "golang.org/x/sys/unix" | |
| ) | |
| func main() { | |
| if len(os.Args) < 2 { | |
| log.Fatalln("Requires atleast one argument") | |
| } | |
| // Example of filePath: /home/scriptnull/cool/code.js | |
| filePath := os.Args[1] | |
| if filePath == "" { | |
| log.Fatalln("Requires a non-empty file path") | |
| } | |
| // Open the filePath | |
| fd, err := unix.Open(filePath, unix.O_RDWR|unix.O_CREAT, 0755) | |
| if err != nil { | |
| log.Fatalf("Error: %+v fd: %d \n", err, fd) | |
| } | |
| // Print the file Descriptor | |
| fmt.Printf("File descriptor value is %d \n", fd) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment