Golang input/output: Difference between revisions

From wikinotes
Line 63: Line 63:


= Filesystem =
= Filesystem =
<blockquote>
== Errors ==
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
</syntaxhighlight>
The old method defines <code>Is${N}</code> functions to test for various <code>os</code> package errors.
<syntaxhighlight lang="go">
// example filesystem operations that create errors
info, err := os.Stat("foo.bar")
fd, err := os.Create("/etc/foo")
// example error checks
if os.IsExist(err) {...}
if os.IsNotExist(err) {...}
if os.IsPermission(err) {...}
</syntaxhighlight>
</blockquote><!-- Errors -->
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
import "os"
import "os"
Line 76: Line 95:
os.MkdirTemp
os.MkdirTemp


_, err := os.Stat("foo.bar")
os.IsExist(err)
os.IsNotExist(err)
os.IsPermission()


os.Remove
err := os.Remove("/var/tmp/foo.bar")
os.RemoveAll
err := os.RemoveAll("/var/tmp/foo")
os.Rename
err := os.Rename("a", "b")


// XDG spec
// XDG spec
os.UserHomeDir
dir, err := os.UserHomeDir()
os.UserConfigDir
dir, err := os.UserConfigDir()
os.UserCacheDir
dir, err := os.UserCacheDir()




os.Getwd
dir, err := os.Getwd()
os.Hostname
host, err := os.Hostname()
</syntaxhighlight>
</syntaxhighlight>



Revision as of 12:10, 18 June 2022

Documentation

fmt https://pkg.go.dev/fmt@go1.18.2
io/ioutil (read/write) https://pkg.go.dev/io/ioutil@go1.18.3
io/fs (filesystem) https://pkg.go.dev/io/fs@go1.18.3
textproto (sockets) https://pkg.go.dev/net/textproto@go1.18.3

print

Basics

require "fmt"

fmt.Println("foo")                 // print to stdout with newline
fmt.Printf("%v", 123)              // print formatted string to stdout (no newline)
fmt.Fprintf(os.Stdout, "%v", 123)  // prints formatted string to writable object (ex. STDOUT, STERR, ..)

fmt.Sprintf("%v", 123)             // returns formatted string (no newline)

Format Syntax

Printf/Sprintf/Fprintf all take format specifiers.
See full docs here, but here's some really useful formats:

# general
%v  # value
%T  # type

# number-bases
%b  # binary
%x  # hex
%o  # octal
%d  # decimal

# number types
%i  # int
%f  # float

# strings
%s  # string
%q  # quoted/escaped go string
%c  # unicode-char for num

stdin, stdout, stderr

Filesystem

Errors

The old method defines Is${N} functions to test for various os package errors.

// example filesystem operations that create errors
info, err := os.Stat("foo.bar")
fd, err := os.Create("/etc/foo")

// example error checks
if os.IsExist(err) {...}
if os.IsNotExist(err) {...}
if os.IsPermission(err) {...}
import "os"

os.Chmod
os.Chown

os.Link

os.Mkdir
os.MkdirAll
os.MkdirTemp


err := os.Remove("/var/tmp/foo.bar")
err := os.RemoveAll("/var/tmp/foo")
err := os.Rename("a", "b")

// XDG spec
dir, err := os.UserHomeDir()
dir, err := os.UserConfigDir()
dir, err := os.UserCacheDir()


dir, err := os.Getwd()
host, err := os.Hostname()
File.Create
File.CreateTemp
File.NewFile
File.Open

File.Stat
File.FileInfo

File.Write
File.WriteString

Files

require "os"

os.WriteFile("/var/tmp/foo.txt", []byte("abc"), 0644)

conts, err := os.ReadFile("/var/tmp/foo.txt")

Networking

Sockets

net.Dial() creates sockets of various types.

import "net"

// unix socketfile
conn, err = net.Dial("unix", "/var/tmp/foo.sock")

// inet socket
conn, err = net.Dial("tcp", "10.10.10.10:6600")
defer conn.Close()

// sending message to socket
_, err = conn.Write([]byte("search title 'it ceases to be'"))
reply := make([]byte, 1024)
_, err = conn.Read(reply)
fmt.Println(string(reply))

HTTP