Categories
My Go Tutorial os Standard Library

os package Reading from the opened file.

func (*File) Read

func (f *File) Read(b []byte) (n int, err error)
// Read reads up to len(b) bytes from the File.
// It returns the number of bytes read and any error encountered.
// At end of file, Read returns 0, io.EOF.
func (f *File) Read(b []byte) (n int, err error) {
	if err := f.checkValid("read"); err != nil {
		return 0, err
	}
	n, e := f.read(b)
	return n, f.wrapErr("read", e)
}

Read method, one take []byte slice argument. Return two values namely n int type and err error type.

In this example, an assumption that the file already opens with os.Open(). a make initializes the variable on a slice of []byte type with has a quantity of 100 of memory of bytes size (uint8 – unsigned 8-bit integers).

data := make([]byte, 100)
count, err := file.Read(data)
if err != nil {
	log.Fatal(err)
}
fmt.Printf("read %d bytes: %q\n", count, data[:count])

file.Read(data) read a stream of 100 bytes into the count variable contains the number in int type and err variable contains the error value

Note: file contents may have CR character, will be counted

Categories
My Go Tutorial os Standard Library

os package with reading an open file and error log

package os

import (
	"errors"
	"internal/poll"
	"internal/testlog"
	"io"
	"runtime"
	"syscall"
	"time"
)

the os package import a few other packages.

file, err := os.Open("file.go") // For read access.
if err != nil {
	log.Fatal(err)
}
// Open opens the named file for reading. If successful, methods on
// the returned file can be used for reading; the associated file
// descriptor has mode O_RDONLY.
// If there is an error, it will be of type *PathError.
func Open(name string) (*File, error) {
	return OpenFile(name, O_RDONLY, 0)
}

Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. If there is an error, it will be of type *PathError.

type File
File represents an open file descriptor.

type File struct {
    // contains filtered or unexported fields
}
file, err := os.Open("file.go") // For read access.

file variable is the file descriptor which has mode O_RDONLY

Note: Next, read contents of pointer type by file variable name

Categories
os

https://golang.org/pkg/os Args variable

Args hold the command-line arguments, starting with the program name.

var Args []string
package main

import (
    "fmt"
    "os"
)

func main() {
    args := os.Args

  // This call will print
  // all command line arguments.
    fmt.Println(args)
}

package main

import (
    "fmt"
    "os"
)

func main() {
    args := os.Args
   
    // The first argument args[0], zero item from slice
    // is the name of the program
    fmt.Printf("The Program Name is: %s \n", args[0])
}
package main

import (
	"fmt"
	"os"
)

func main() {
	args := os.Args
	/* for i := 0; i < len(args); i++ {
		fmt.Println(args[i])
	} */
	
	// same as above but using for _,_ :=  range []slice
	for _, value := range args {
		fmt.Println(value)
	}
}