码迷,mamicode.com
首页 > 其他好文 > 详细

An Introduction to Programming in Go - v1

时间:2015-05-21 23:56:56      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

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; appendcopy;

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

1 package math
2 
3 import "testing"
4 
5 func TestAverage(t *testing.T) {
6 
7 ...
8 
9 }

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

An Introduction to Programming in Go - v1

标签:

原文地址:http://www.cnblogs.com/windschar/p/4518593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!