Skip to content

Instantly share code, notes, and snippets.

@scriptnull
Created February 28, 2018 20:22
Show Gist options
  • Select an option

  • Save scriptnull/e8d2b42b364bf10a18402ba8dabb8267 to your computer and use it in GitHub Desktop.

Select an option

Save scriptnull/e8d2b42b364bf10a18402ba8dabb8267 to your computer and use it in GitHub Desktop.
Opens a file and prints the file descriptor
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