"Disclaimer: Copyright All, welcome reprint, do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
Learning a new language is nothing more than starting with the main grammar. It's boring to learn a language through grammar books, so we'd better start learning a new language from the simplest examples. There are few examples, but they are representative.
(a) The simplest code
Package Mainimport "FMT" Func Main () { fmt. Println ("Hello, World")}
(b) The main function
Package Mainimport "FMT" func sub (a int, b int) int { return a-B;} Func Main () { fmt. Println (Sub (2, 3))}
(c) If statement learning
Package Mainimport "FMT" func compare (a int, b int) { if (a > B) { FMT. Println ("Greater") }else{ FMT. Println ("smaller") }}func Main () { compare (3, 2)}
(d) switch statement learning
Package Mainimport "FMT" func Test (a int.) { switch (a) {case 1: FMT. Println ("1") Case 2: FMT. Println ("2") default: FMT. PRINTLN ("error") }}func Main () { Test (1) Test (2) Test (3)}
(e) cyclic sentence learning
Package Mainimport "FMT" func Show (data int) { var index int index = 0 for { if (index >= data) { bre AK } FMT. PRINTLN (Index) index + + continue }}func Main () { Show (10)}
(f) Structural learning
Package Mainimport "FMT" type node struct { data Int}func (p* node) set (val int) () { p.data = Val}func (p* node) get () int { return p.data;} Func Main () { N: = node{data:10} m: = &n m.set () FMT. Println (M.get ())}
Say so much first, next time to introduce a bit more advanced features.
Fantastic go language (basic grammar)