@mSolo
        
        2015-05-07T03:09:19.000000Z
        字数 16456
        阅读 3756
    Go Golang
bin/todo # command executablepkg/linux_amd64/code.google.com/p/goauth2/ # reflect your OS and architectureoauth.a # package objectgithub.com/nf/todo/ task.asrc/code.google.com/p/goauth2/.hg/ # mercurial repository metadataoauth/oauth.go # package sourceoauth_test.go # test sourcegithub.com/nf/todo/.git/task/task.go # package sourcetodo.go

package mainimport "fmt"func main() {fmt.Printf("hello, world\n")}
① $ go run helloworld.go 
② $ 6g helloworld.go ---> $ 6l helloworld.6 ---> $ ./6.out
$HOME/HelloWorldWorkspace/bin/helloworldpkg/src/github.com/msolo/helloworld.githelloworld.go$ export GOPATH=$HOME/HelloWorldWorkspace$ go install github.com/msolo/helloworld$ $GOPATH/bin/helloworldhello, world
package newmath// Sqrt returns an approximation to the square root of x.func Sqrt(x float64) float64 {z := 1.0for i := 0; i < 1000; i++ {z -= (z*z - x) / (2 * z)}return z}
package mainimport (fm "fmt""github.com/user/newmath")func main() {fm.Printf("Hello, world. Sqrt(2) = %v\n", newmath.Sqrt(2))}
$ cd $HOME/HelloWorldWorkspace/bin/helloworldpkg/linux_amd64/github.com/user/newmath.asrc/github.com/user/helloworldhelloworld.gogithub.com/user/newmathsqrt.go
$GOROOT/pkg/$GOOS_$GOARCH/$ go get -v code.google.com/p/go.tools/cmd/godoc       # 安装$ godoc -http=:8888 -goroot=$GOROOT            # Open http://localhost:8888$ godoc cmd/go$ godoc –src fmt Printf$ gofmt –w program.goinit()
const str = "str"var identifier int (= 5)name := "Jack"a, b, c := 5, 7, "abc"type (IZ intSTR string)var a IZ = 5b := int32(a)const (Sunday = iota // Sunday = 0Monday // 1...)var (a intb bool)var (a = 15b = false)
var Identifier1 string        // this "object" is said to be exportedvar identifier2 string        // not visible, like privatefmt.Print() | fmt.Println() ---> fmt.Print("Hello:", 23) fmt.Printf()&&fmt.Sprintf()
%v ---> the value in a default format. when printing structs, the plus flag (%+v) adds field names
%T ---> a Go-syntax representation of the type of the value
%t ---> the word true or false
%c ---> the character represented by the corresponding Unicode code
%x ---> base 16, with lower-case letters for a-f
%U ---> Unicode format: U+1234; same as "U+%04X"
%g ---> whichever of %e or %f produces more compact output

This is a raw string\n
fmt.Println(strings.ContainsAny("failure", "u & i"))fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) // ["foo" "bar" "baz"]s := []string{"foo", "bar", "baz"}fmt.Println(strings.Join(s, ", ")) // foo, bar, bazfmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) // moo moo moofmt.Printf("%q\n", strings.Split("a,b,c", ",")) // ["a" "b" "c"]fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) // ["a," "b," "c"]fmt.Println(strings.Title("her royal highness")) // Her Royal Highnessfmt.Printf("[%q]", strings.Trim(" !!! Achtung! Achtung! !!! ", "! ")) // ["Achtung! Achtung"]fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n")) // a lone gopher
func Atoi(s string) (i int, err error)func Itoa(i int) string// accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, Falsefunc ParseBool(str string) (value bool, err error)// 解析小数点后6位,不能带其它字符func ParseFloat(s string, bitSize int) (f float64, err error)func ParseInt(s string, base int, bitSize int) (i int64, err error)
const (ANSIC = "Mon Jan _2 15:04:05 2006"UnixDate = "Mon Jan _2 15:04:05 MST 2006"RubyDate = "Mon Jan 02 15:04:05 -0700 2006"RFC822 = "02 Jan 06 15:04 MST"RFC822Z = "02 Jan 06 15:04 -0700" // with zoneRFC850 = "Monday, 02-Jan-06 15:04:05 MST"RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700"RFC3339 = "2006-01-02T15:04:05Z07:00"RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"Kitchen = "3:04PM"Stamp = "Jan _2 15:04:05"StampMilli = "Jan _2 15:04:05.000"StampMicro = "Jan _2 15:04:05.000000"StampNano = "Jan _2 15:04:05.000000000")time.Secondtime.Januarytime.Sundayfmt.Println(t.Format(time.RFC822))const shortForm = "2006-01-02"t, _ = time.Parse(shortForm, "2013-Feb-03")AddDate(-1, 2, 3) applied to 2011-01-01 ---> 2010-03-04oneday, _ := time.ParseDuration("24h")
Built-in function :
len、cap、new、make、copy、append、panic、recover
defer: a) 多个defer工作方式: 后进先出(LIFO);b) tracing
g := func(i int) { fmt.Printf("%d\n", i) }type binOp func(int, int) (int, int, int)someFunc := binOpfunc getX2AndX3_2(input int) (x2 int, x3 int) { // 命名返回值//...}func min(a ...int) int { ... }arr := []int{7, 9, 3, 5, 1}x := min(arr...) // x := min(7, 9, 3, 5, 1)func(x, y int) int {return x + y}(3, 4)func main() {printHeader()defer printFooter()}
var identifier []type | var arr1 [5]int ---> len(arr1) is 5slice仍保持着对array的引用,那么整个array就将留在内存中直到解除引用
var arrAge = [5]int{18, 20, 15, 22}var arrLazy2 = []int{5, 6, 7, 8, 22}var arrKeyValue1 = [5]string{3: "Chris", 4: "Ron"}var arrKeyValue2 = []string{3: "Chris", 4: "Ron"}var slice1 []type = arr1[start:end]var slice2 []type = arr1[:] // &arr1// creating a slice with makevar slice1 []type = make([]type, len, cap)var slice2 []int = make([]int, 10)// reslicings1 = s1[0:len(s1)+1]// copying and appending slicess1_from := []int{1, 2, 3}s1_to := make([]int, 10)n := copy(s1_to, s1_from) // [1 2 3 0 0 ... 0]s13 := []int{1, 2, 3}s13 = append(s13, 4, 5, 6) // [1 2 3 4 5 6]sort.Ints(arr1)sort.IntsAreSorted(arr1)sort.Float64s(arr2)func SearchStrings(a []string, x string) intfunc Strings(a []string) // sorts a slice in increasing order
var mapVariable map[keytype]valuetypevar mapVariable[keytype]valuetype = make(map[keytype]valuetype)
map1 := make(map[string]float)map2 := map[string]float{}mf := map[int]func() int {1: func() int { return 10 },5: func() int { return 50 },}delete(map1, key1)
ok, _ := regexp.Match(pat, []byte(searchIn))ok, _ := regexp.MatchString(pat, searchIn)// func Compile(expr string) (*Regexp, error)re := regexp.MustCompile("fo.?")fmt.Printf("%q\n", re.FindString("seafood")) // "foo"// func (re *Regexp) Split(s string, n int) []strings := regexp.MustCompile("a*").Split("abaabaccadaaae", 5)searchIn := "John: 2578.34 William: 4567.23 Steve: 5632.18"pat := "[0-9]+.[0-9]+"f := func(s string) string {v, _ := strconv.ParseFloat(s, 32)return strconv.FormatFloat(v * 2, 'f', 2, 32)}re, _ := regexp.Compile(pat)str1 := re.ReplaceAllString(searchIn, "##.#")str2 := re.ReplaceAllStringFunc(searchIn, f)
type struct1 struct {i1 intf1 float32str string}func main() {ms := new(struct1) // ms := &struct1{10, 15.5, “Chris”}ms.i1 = 10 // var mt struct1ms.f1 = 15.5 // mt = struct1{10, 15.5, “Chris”}ms.str = "Chris"}type B struct {thing int}func (b *B) write() string { return fmt.Sprint(b) }/* -----------------------** multiple inheritance ** -----------------------*/type camera struct { }func (c *camera) TakeAPicture() string {return "click"}type phone struct { }func (p *phone) Call() string {return "Ring Ring"}type cameraPhone struct {cameraphone}cp := new(cameraPhone)cp.TakeAPicture()cp.Call()
type TagType struct {field1 bool "An important answer"field2 string "The name of the thing"field3 int "How much there are"}func main() {tt := TagType{true, "Barak Obama", 1}for i:= 0; i< 3; i++ {reflectTag(tt, i)}}func reflectTag(tt TagType, ix int) {ttType := reflect.TypeOf(tt)ixField := ttType.Field(ix)fmt.Printf("%v\n", ixField.Tag)}
interfaces in Go are short, they usually have from 0 ~ 3 methods.
type Shaper interface {Area() float32}type Square struct {side float32}func (sq *Square) Area() float32 {return sq.side * sq.side}func main() {sq1 := new(Square)sq1.side = 5areaIntf := Shaper(sq1) // areaIntf := sq1fmt.Printf(“%f\n”, areaIntf.Area())}func (r Rectangle) Area() float32 { // type Rectangle struct { length, width float32 }return r.length * r.width}r := Rectangle{5, 3}q := &square{5}shapes := []Shaper{r, q}for n, _ := range shapes {fmt.Println("Area is: ", shapes[n].Area())}
type valuable interface {getValue() float32}type stockPosition struct {ticker stringsharePrice float32count float32}func (s stockPosition) getValue() float32 {return s.sharePrice * s.count}type car struct {make stringmodel stringprice float32}func (c car) getValue() float32 {return c.price}func showValue(asset valuable) {fmt.Printf(“%f\n”, asset.getValue())}func main() {var o valuable = stockPosition("GOOG", 577.20, 4)showValue(o)o = car{"BMW", "M3", 66500}showValue(o)}
type ReadWrite interface {Read(b Buffer) boolWrite(b Buffer) bool}type Lock interface {Lock()Unlock()}type File interface {ReadWriteLockClose()}
func classifier(items ...interface{}) {for i, x := range items {switch x.(type) {case bool: ...case int: ...case nil: ...}}}classifier(13, -14.3, “BELGIUM”, nil, false)
type specialString stringvar whatIsThis specialString = "hello"testFunc := func(any interface{}) {switch v := any.(type) {case string: fmt.Prinf("any %v is a string type", v)case specialString: fmt.Printf("a specialString type")//...}}
type Appender interface {Append(int)}type Lener interface {Len() int}type List []intfunc (l List) Len() int { // Value-receiver methodreturn len(l)}func (l *List) Append(val int) {*l = append(*l, val)}func CountInto(a Appender, start, end int) {for i := start; i <= end; i++ {a.Append(i)}}func LongEnough(l Lener) bool {return l.Len()*10 > 42}func main() {var lst List // A bare value// compiler error:// cannot use lst (type List) as type Appender in function// argument: List does not implement Appender (Append method requires pointer receiver)// CountInto(lst, 1, 10),if LongEnough(lst) { // VALID: Identical receiver typefmt.Printf("- lst is long enough")}plst := new(List) // A pointer valueCountInto(plst, 1, 10) // VALID: Identical receiver typeif LongEnough(plst) {fmt.Printf("- plst is long enough")}}var ai AbsInterface // declares method Abs()type SqrInterface interface {Sqr() float32}var si SqrInterfacepp := new(Point) // implement AbsInterface & SqrInterfacevar empty interface{}empty = pp; // everything satisfies emptyai = empty.(AbsInterface)si = ai.(SqrInterface)
var x float64 = 3.4fmt.Println("type:", reflect.TypeOf(x)) // type: float64v := reflect.ValueOf(x)fmt.Println("value:", v) // value: <float64 Value>fmt.Println("type:", v.Type()) // type: float64fmt.Println("kind:", v.Kind()) // kind: float64fmt.Println("value:", v.Float()) // value: 3.4 <-------------------fmt.Println(v.Interface()) // 3.4y := v.Interface().(float64) // <-------------------fmt.Println(y) // 3.4reflect.ValueOf(x)v = reflect.ValueOf(&x)v.CanSet()v.SetFloat(3.1415)t := T{23, "skidoo"}s := reflect.ValueOf(&t).Elem()s.NumField()s.Field(0)
for i:= 0; i < 10; i++ {a := rand.Int()fmt.Printf("%d\n", a)}for I := 0; i < 5; i++ {r := rand.Int(8)fmt.Printf("%d\n", r)}fmt.Println()timens := int64(time.Now().Nanosecond())rand.Seed(timens)fmt.Printf("r2.2f\n", 100*rand.Float32())
func Fibonacci(n int) (res int) {if n <= 1 {res = 1} else {res = fibonacci(n-1) + Fibonacci(n-2)}return}
// var r *bytes.Buffer = new(bytes.Buffer)var buffer bytes.Bufferfor {if s; ok := getNextString(); ok {buffer.WriteString(s) // append} else {break}}fmt.Print(buffer.String(), "\n")
s1 := "hello"c := []byte(s1)c[0]= 'c's2 := string(c)
fmt.Scanln(&input)fmt.Scanf("%s %s", &firstName, &lastName)fmt.Sscanf(“56.12 / 5212 / Go”, "%f / %d / %s", &f, &i, &s)
var inputReader *bufio.Readervar input stringvar err errorfunc main() {inputReader = bufio.NewReader(os.Stdin)fmt.Println("Please enter some input: ")input, err = inputReader.ReadString('\n') // '\n' is included!!!if err == nil {fmt.Printf("The input was: %s", input)}}switch input { case "Philip\r\n": ... }buf := make([]byte, 1024) n, err := inputReader.Read(buf)
inputFile, inputError := os.Open("input.dat")defer inputFile.Close()inputReader := bufio.NewReader(inputFile)for {inputString, readerError := inputReader.ReadString('\n')// ...}import "io/ioutil"// ...inputFile, outputFile := "products.txt", "products_copy.txt"buf, err := ioutil.ReadFile(inputFile)if err != nil {fmt.Fprintf(os.Stderr, "File Error: %s\n", err)}err = ioutil.WriteFile(outputFile, buf, 0x644)if err != nil {panic(err.Error())}file, err := os.Open(“products2.txt”) // separated by space// ...var col1, col2, col3 []stringfor {var v1, v2, v3 string_, err := fmt.Fscanln(file, &v1, &v2, &v3)// ...}col1 = append(col1, v1)
import "compress/gzip"// ...var r *bufio.Readerfi, err := os.Open("MyFile.gz")if err != nil {fmt.Fprintf(...)os.Exit(1)}fz, err := gzip.NewReader(fi)if err != nil {r = bufio.NewReader(fi)} else {r = bufio.NewReader(fz)}for {line, err := r.ReadString('\n')// ...}import "io"// ...src, _ := os.Open(srcName)// ...dst, _ := os.OpenFile(...)io.Copy(dst, src)
outputFile, outputError := os.OpenFile("output.dat", os.O_WRONLY|os.O_CREATE, 0666)outputWriter := bufio.NewWriter(outputFile)outputWriter.WriteString(outputString)outputWriter.Flush()os.Stdout.WriteString("hello, world\n")f, _ := os.OpenFile("test", os.O_CREATE|os.O_WRONLY, 0)f.WriteString("hello, world in a file\n") // not buffered
who := "Alice"if len(os.Args) > 1 {who += strings.Join(os.Args[1:], " ")}fmt.Println("Good Morning", who)import "flag"//...var NewLine = flag.Bool(“n”, false, “print on newline”)//...flag.PrintDefaults()flag.Parse()var s string = ""for i := 0; i < flag.NArg(); i++ {s += flag.Arg(i)if *NewLine {s += "\n"} else {s += " "}}$ a.out –n A B C
func NewDecoder(r io.Reader) *Decoderfunc NewEncoder(w io.Writer) *Encoderfunc (dec *Decoder) Decode(v interface{}) error
type Address struct {Type stringCity stringCountry string}...pa := Address{"private", "Aartselaar", "Belgium"}js, _ := json.Marshal(pa) // json.MarshalForHTML(pa)...file, _ := os.OpenFile(“vcard.json”, os.O_CREATE|os.O...)enc := json.NewEncoder(file)err := enc.Encode(vc)b = []byte({"Name": "Wednesday", "Age": 6, "Parents": ["Gomez", "Morticia"]})var f interface{}err := json.Unmarshal(b, &f)map[string]interface{}{ // f.(map[string]interface{})"Name": "Wednesday","Age": 6"Parents": []interface{}{"Gomez","Morticia",},}// func Unmarshal(data []byte, v interface{})type FamilyMember struct {Name stringAge intParents []string}var m FamilyMembererr := json.Unmarshal(b, &m)
import "encoding/xml"
var t, token xml.Tokenvar err errorfunc main() {input := "<Person><FirstName>Laura</FirstName>"input += "<LastName>Lynn</LastName></Person>"inputReader := strings.NewReader(input)p := xml.NewParser(inputReader)for t, err = p.Token(); err == nil; t, err = p.Token() {switch token := t.(type) {case xml.StartElement:name := token.Name.Localfmt.Printf(“Token name: %s\n”, name)for _, attr := range token.Attr {attrName := attr.Name.LocalattrValue := attr.Valuefmt.Printf(“attribute: %s %s\n”, attrName,attrValue)// ...}case xml.EndElement:fmt.Println(“End of token”)case xml.CharData:content := string([]byte(token))fmt.Printf(“The content: %v\n”, content)// ...default: // ...}}}
hasher := sha1.New()io.WriteString(hasher, “test”)b := []byte{}fmt.Printf(“Result: %x\n”, hasher.Sum(b))hasher.Reset()data := []byte(“We shall overcome!”)n, err := hasher.Write(data)if n != len(data) || err != nil {log.Printf(“Hash write error: %v / %v”, n, err)}checksum := hasher.Sum(b)fmt.Printf(“Result: %x\n”, checksum)
type error interface {Error() string}
import “errors”
var errNotFound error = errors.New(“Not found error”)err := errors.New(“math – square root of negative number”)
type PathError struct {Op stringPath stringErr error}
panic(“A severe error occurred: stopping the program!”)package parse// importtype ParseError struct {Index intWord stringError err}func (e *ParseError) String() string {return fmt.Sprintf(“...error parsing %q as int”, e.Word)}func Parse(input string) (numbers []int, err error) {defer func() {if r := recover(); r != nil {var ok boolerr, ok = r.(error)if !ok {err = fmt.Errorf(“pkg: %v”, r)}}}()fields := strings.Fields(input)numbers = fields2numbers(fields)return}func fields2numbers(fields []string) (numbers []int) {if len(fields) == 0 {panic(“no words to parse”)}for idx, field := range fields {num, err := strconv.Atoi(field)if err != nil {panic(&ParseError{idx, field, err})}numbers = append(numbers, num)}return}
package mainimport (“fmt”“./parse/parse”)func main() {var examples = []string{“1 2 3 4 5”, ..., }for _, ex := range examples {fmt.Printf(“Parsing %q:\n “, ex)nums, err := parse.Parse(ex)if err != nil {fmt.Println(err)continue}fmt.Println(nums)}}
package mainimport ("fmt""os/exec""os")func main() {env := os.Environ()procAttr := &os.ProcAttr{Env: env,Files: []*os.File{os.Stdin,os.Stdout,os.Stderr,},}pid, err := os.StartProcess(“/bin/ls”, []string{“ls”, “-l”}, procAttr)if err != nil {fmt.Printf(“Error %v starting process!”, err) //os.Exit(1)}fmt.Printf(“The process id is %v”, pid)}
cmd := exec.Command(“gedit”) // this opens a gedit-windowerr := cmd.Run()if err != nil {fmt.Printf(“Error %v executing command!”, err)os.Exit(1)}fmt.Printf(“The command is %v”, cmd)