Go Language Tutorial Combat

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Install the online tutorials

Because of the wall, golang.org domestic inaccessible, its online tutorial is the same tutorial.golang.org http://go-tour-zh.appspot.com/


Can be run on this machine,

First install the Go compiler http://code.google.com/p/go/downloads/list


Then install the tutorial

Go get Code.google.com/p/go-tour/gotour

or Chinese.

Go get Bitbucket.org/mikespook/go-tour-zh/gotour

the final execution of the installation results in gotour execute the file to open the tutorial in http://localhost:3999.

Some of the answers to the exercises


#46 Exercise: Fibonacci Closures

Package Mainimport the "FMT"//Fibonacci function returns a function that returns INT. Func Fibonacci () func () int {var a int = 1var b int = 1return func () int {c: = A+ba = BB = Creturn c}}func Main () {f: = fi Bonacci () for I: = 0; I < 10; i++ {fmt. Println (f ())}}


#69 Exercise: equivalent binary tree

Package Mainimport ("Bitbucket.org/mikespook/go-tour-zh/tree", "FMT")/*type tree struct {left  *treevalue intright * tree}*///Walk Step tree T sends all values from the tree to channel Ch. Func Walk (t *tree. Tree, ch Chan int) {if T! = Nil {Walk (t.left, ch) ch <-t.valuewalk (t.right, ch)}}//same detects if the tree T1 and T2 contain the same value. Func Same (t1, T2 *tree. Tree) bool {ch1: = make (chan int, ten) go Walk (T1, ch1) CH2: = Make (chan int, ten) go Walk (T2, CH2) for I: = 0; i <; i++ { If <-ch1! = <-ch2 {return False}}return True}func main () {FMT. Println (same, tree. New (1), tree. New (1))}


#57 Exercise: Error

Package Mainimport ("FMT") type errnegativesqrt Float64func (e errnegativesqrt) Error () string {return FMT. Sprintf ("Cannot Sqrt negative number:%f",  e)}func Sqrt (f float64) (Float64, error) {if f < 0 {return 0, Errnegativ Esqrt (f)}x: = ffor I: = 0; I < 10; i++ {x = (x + f/x)/2}return x, Nil}func main () {FMT. Println (SQRT (2)) fmt. Println (Sqrt (-2))}

#44 Exercise:Map


Package Mainimport (//"FMT" "Strings" "BITBUCKET.ORG/MIKESPOOK/GO-TOUR-ZH/WC") func WordCount (s string) map[string]int {result: = map[string]int {}f: = strings. Fields (s) for _, V: = range F {result[v] + = 1}return Result}func main () {WC. Test (WordCount)}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.