An Introduction to Programming in Go - v1
2015-05-21 22:04
495 查看
Types
int, uint, float32, float64, bool, stringControl Structures
if-elsefor
switch
Arrays, Slices and Maps
Arrays - indexable, single type, fixed lengthSlices - indexable, single type but length is allowed to change; use make function to craete a slice; append; copy;
Maps - unordered, key-value pairs; initialize before use; make; delete;
Functions
named return typemultiple returned values
variadic parameters
closure - create functions inside function
recursion
defer - scheduled a function call to be run after the function completes; often used for resources releasing;
panic - indicates a programmer error
recover - handle a runtime panic
Pointers
pointers reference a location in memory where a value is stored rather than the value itselfpointer is represented using the * character followed by the type of the stored value
* is also used to "dereference" pointer variables
use & operator to find the address of a variable
new - take a type as an argument, allocates enough memory to fit a value of that type, returns a pointer to it
Structs and Interfaces
struct - a type which contains named fileds; create methods for structsinterface - not like struct contains fields, in interface we define "method sets"
Concurrency
goroutine - use keyword go followed by a function invocation to create a goroutine; the first goroutine is implicit and is the main function itself;channel - it provides a way for two goroutines to communicate with one another and synchronize their executions
channel directions - specify a direction on a channel type thus restricting it either sending or receiving
select - works like switch but for channels; often used to implement a timeout;
time.after
buffered channel - specifiy capacity
packages
testing
package math import "testing" func TestAverage(t *testing.T) { ... }
Core Packages
strings - a large number of functions working with stringsinput/output - io package consists of a few functions, but mostly interfaces used in other packages; two main interfaces are Reader and Writer.
files & folders - os package
errors - errors package; error.New("...")
containers & sort
container/list - implement a doubly-linked list
sort - contains functions for sorting arbitrary data; sort.Interface requires 3 methods: Len, Less, Swap, any type defined these 3 methods can be sorted.
hashes & cryptography - non-cryptographic hash functions in hash/adler32, crc32, crc64, fnv; cryptographic hash functions in crypto/sha1, crypto/md5, etc.
servers - net, net/http, net/rpc, etc
parsing command line args - flag package
synchronization primitives - Go provides more traditional multithreading routines in sync and sync/atomic packages
相关文章推荐
- An Introduction to Interactive Programming in Python
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 3 Stopwatch
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习
- An Introduction to Interactive Programming in Python (Part 2) week 6 (Classes and Tied images)
- Quiz 6a Question 7————An Introduction to Interactive Programming in Python
- Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —"Pong"
- 【python】An Introduction to Interactive Programming in Python(week two)
- An Introduction to Interactive Programming in Python 第四课作业 Pong
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_2 练习
- An Introduction to Interactive Programming in Python (Part 1) -- Week 2_3 练习
- Quiz 6a Question 7————An Introduction to Interactive Programming in Python
- An Introduction to Interactive Programming in Python(week 6)
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 7 Spaceship
- [Coursera][Rice] An introduction to interactive Programming in Python Week 2 3
- An Introduction to Interactive Programming in Python 第五课作业 memory
- Coursera_An Introduction to Interactive Programming in Python_Mini-project # 4 Pong
- An Introduction to Interactive Programming in Python (Part 1) - Week 1
- Quiz 6b Question 7————An Introduction to Interactive Programming in Python
- An Introduction to Interactive Programming in Python 第六课作业 Blackjack
- Quiz 6b Question 7————An Introduction to Interactive Programming in Python