您的位置:首页 > 编程语言 > Go语言

An Introduction to Programming in Go - v1

2015-05-21 22:04 495 查看

Types

int, uint, float32, float64, bool, string

Control Structures

if-else

for

switch

Arrays, Slices and Maps

Arrays - indexable, single type, fixed length

Slices - 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 type

multiple 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 itself

pointer 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 structs

interface - 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 strings

input/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐