A personal journey of fun and experiments with the Go language.
Following are some of the functions, variables and commands that I am personally using along my journey with Go.
- len(v Type) int
- bufio.NewScanner(r io.Reader) *Scanner
- bufio: (s *Scanner).Scan() bool
- bufio: (s *Scanner).Text() string
- fmt.Println(a ...interface{}) (n int, err error)
- fmt.Sprintf(format string, a ...interface{}) string
- strings.Join(elems []string, sep string) string
- os.Args []string
- os.Stdin NewFile(uintptr(syscall.Stdin), "/dev/stdin")
- runtime.NumCPU() int
- time.Now() Time
- time.Since(t Time) Duration
- break
- case
- chan
- const // example: const World = "世界"
- default
- defer
- else
- for
- func
- go
- if
- import
- interface
- map
- package
- range
- return
- select
- struct
- switch
- type // example: switch v := i.(type)
- var
keywords pool
- goto
- fallthrough
- continue
- go [command] [arguments]
- go build [packages]
- go doc [pkg] [methodOrField]
- go fmt [filename]
- go get [packages] (-v:verbose)
- go help [keyword]
- go run [filename]
- go test -bench=. [filename]
- go version
- pointers
- a pointer holds the memory address of a value
- the & operator generates a pointer to its operand
- the * operator denotes a pointer's underlying value
%d | int in decimal |
%x, %o, %b | int in hex, oct, bin |
%f, %g, %e | float |
%t | bool |
%c | rune |
%s | string |
%q | quoted string, rune |
%v | any value |
%T | type |
%% | literal percent |
- switch - see switch condition
- switch evaluation order - see evaluation in case
- switch with no condition - see no condition
- pointers - unerstand pointer, &, *
- struct pointers - see struct field change via pointer
- a slice consists of a pointer to the array, the length of the segment, and its capacity
- slice length & capacity - see how underlying array capacity changes when array elements are removed from front of array
- create slice with make - reinforce understanding of how removing array elements from beginning affect underlying array capacity
- pointer receivers
- pointers & functions
- methods and pointer indirection - methods with pointer receivers take either a value (as a convenience) or a pointer
- therefore method pointer receivers can modify the received value even if it was passed as a value instead of a pointer
- all methods on a given type should have either value or pointer receivers, but not a mixture of both
- a value of interface type can hold any value that implements those methods
- an interface value holds a value of a specific underlying concrete type
- if the concrete value inside the interface itself is nil, the method will be called with a nil receiver
- calling a method on a nil interface is a run-time error - there has to be some type with a concrete method assigned to the interface value
- an empty interface may hold values of any type
- type assertion provides access to an interface value's underlying concrete value
- type assertion syntax is similar to that of reading from a map
- the error type is a built-in interface similar to fmt.Stringer
- as with fmt.Stringer, the fmt package looks for the error interface when printing values
- only the sender should close a channel, never the receiver
- fibonacci with scalar array and channels - practice
- closing a channel is only necessary when the receiver must be told there are no more values coming, such as to terminate a range loop
type Stringer interface {
String() string
}
type error interface {
Error() string
}
- there are many implementations of the io.Reader interface, including files, network connections, compressors, ciphers, and others