The Magic Go Language (basic grammar)

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


"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


Learning a new language is nothing more than starting with the basic grammar. It is boring to learn a language through grammar books, so we may as well 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) Basic functions

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.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.