Golang input/output: Difference between revisions

From wikinotes
Line 14: Line 14:
</blockquote><!-- Documentation -->
</blockquote><!-- Documentation -->


= stdin, stdout, stderr =
= Stdin, Stdout, Stderr =
<blockquote>
<blockquote>
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">

Revision as of 16:19, 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

Stdin, Stdout, Stderr

fmt.Fprintln(os.Stdout, "writes stdout")
fmt.Fprintln(os.Stderr, "writes stderr")

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