gpl

module
v0.0.0-...-fce9d62 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2019 License: MIT

README

The Go Programming Language
Brian W. Kernighan, Alan A. A. Donovan
Addison-Wesley Professional
2015

1. Tutorial

1.1 Hello, World

compiled

go run
go build
go get

unicode

package <--> {1+} *.go

main package
main function

Must import exact packages, no more, no less.

package
imports
declarations
    func
    var
    const
    type
functions

Semicolons are not required, except when multiple statements appear on the same line.

gofmt
go fmt

goimports

1.2 Command-Line Arguments

os package
os.Args, a slice of strings
s[i]
s[m:n], 0<=m<=n<=len(s), containing n-m elements
m defaults to 0 if omitted
n defaults to len(s) if omitted

os.Args[0] is the name of the command.
os.Args[1:] are the arguments.

comments begin will //
package comments precedes package declaration
var can be explicitly initialized as port of declaration, or implicitly to zero value of its type.

+ operator can concatenate strings
+= assignment operator
:= short variable declaration
postfix only, i++ not ++i

for initialization; condition; post {
    // zero or more statements
}

Braces are mandatory, the rest are optional.

initialization must be a simple statement
    a short variable declaration
    an increment or assignment statement
    a function call

for can also iterate over a range.

unused local variable is forbidden.

blank identifier _

ways to declare a string
    s := "" // only in functions, not for package level
    var s string // relies on default
    var s = "" // rarely used
    var s string = "" // explicit if need different value from default

    preference: 1, 2, 4, 3

1.3 Finding Duplicate Lines

Parentheses are not used around the condition of if statement, but
brances are required for the body.

A map
    holds a set of key-value pairs
    provides constant-time operations
    needs a key type that is comparable using ==
    iterates in unspecified/random order

bufio.Scanner
bufio.Scanner.Scan()
    reads the next line
    tripping the newline character
    returns true if there is a line
    returns false if no more input

fmt.Printf
    format using verbs
    escape sequences, e.g. \t, \n
    no newline by default

Printf, no newline
    fmt.Printf
    log.Printf
    fmt.ErrorF

Println, newline
    fmt.Println

os.Open() returns
    *os.File
    error

Functions and other package-level entities may be declared in any order.

A map is passed by reference.

ioutil.ReadFile
string.Split

*os.File provides low level methods Read and Write used by
    bufio.Scanner
    ioutil.ReadFile
    ioutil.WriteFile

1.4 Animated GIFs

Lissajous figures

refer to package with the last component of a path
    gif.GIF belongs to image/gif

const may appear
    at package level
    within a function
const must be
    number
    string
    boolean

composite literals
    slice []color.Color{...}
    struct gif.GIF{...}

struct groups fields

builtin append function

1.5 Fetching a URL

net/http

1.6 Fetching URLs Concurrently

A goroutine is a concurrent function execution.
A channel is a communication mechanism among goroutines.
main runs in a goroutine.
go satement creates additional goroutines.

A goroutine blocks when send or receive on a channel until
another goroutine receives or sends correspondingly.

Directories

Path Synopsis
ch01
dup1
Dup1 prints the text of each line that appears more than once in the standard input, preceded by its count.
Dup1 prints the text of each line that appears more than once in the standard input, preceded by its count.
dup2
Dup2 prints the count and text of lines that appear more than once in the input.
Dup2 prints the count and text of lines that appear more than once in the input.
echo1
Echo1 prints its command-line arguments.
Echo1 prints its command-line arguments.
echo2
Echo2 prints its command-line arguments.
Echo2 prints its command-line arguments.
echo3
Echo3 prints its command-line arguments.
Echo3 prints its command-line arguments.
echo4
Echo4 prints its command-line arguments.
Echo4 prints its command-line arguments.
ex1.1
Exercise 1.1: Modify the echo program to also print os.Args[0], the name of the command that invoked it.
Exercise 1.1: Modify the echo program to also print os.Args[0], the name of the command that invoked it.
ex1.10
Exercise 1.10: Find a web site that produces a large amount of data.
Exercise 1.10: Find a web site that produces a large amount of data.
ex1.2
Exercise 1.2: Modify the echo program to print the index and value of each of its arguments, one per line.
Exercise 1.2: Modify the echo program to print the index and value of each of its arguments, one per line.
ex1.3
Exercise 1.3: Experiment to measure the difference in running time between our potentially inefficient versions and the one that uses strings.Join.
Exercise 1.3: Experiment to measure the difference in running time between our potentially inefficient versions and the one that uses strings.Join.
ex1.4
Exercise 1.4: Modify dup2 to print the names of all files in which each duplicated line occurs.
Exercise 1.4: Modify dup2 to print the names of all files in which each duplicated line occurs.
ex1.5
Exercise 1.5: Change the Lissajous program’s color palette to green on black, for added authenticity.
Exercise 1.5: Change the Lissajous program’s color palette to green on black, for added authenticity.
ex1.6
Exercise 1.6: Modify the Lissajous program to produce images in multiple colors by adding more values to palette and then displaying them by changing the third argument of SetColorIndex in some interesting way.
Exercise 1.6: Modify the Lissajous program to produce images in multiple colors by adding more values to palette and then displaying them by changing the third argument of SetColorIndex in some interesting way.
ex1.7
Exercise 1.7: The function call io.Copy(dst, src) reads from src and writes to dst.
Exercise 1.7: The function call io.Copy(dst, src) reads from src and writes to dst.
ex1.8
Exercise 1.8: Modify fetch to add the prefix http:// to each argument URL if it is missing.
Exercise 1.8: Modify fetch to add the prefix http:// to each argument URL if it is missing.
ex1.9
Exercise 1.9: Modify fetch to also print the HTTP status code, found in resp.Status.
Exercise 1.9: Modify fetch to also print the HTTP status code, found in resp.Status.
fetch
Fetch prints the content found at a URL.
Fetch prints the content found at a URL.
fetchall
Fetchall fetches URLs in parallel and reports their times and sizes.
Fetchall fetches URLs in parallel and reports their times and sizes.
lissajous
Lissajous generates GIF animations of random Lissajous figures.
Lissajous generates GIF animations of random Lissajous figures.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL