Go Learning Notes

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

    • Go language
      • Characteristics
        • The word of Praise
      • Chapter1
        • Tips
      • Chatper2 Basic Syntax
        • Special reserved Keywords
        • Tips
        • Variable request
        • Built-in types
        • Process Control
          • If
          • For
          • Break Goto can support tags
          • Switch
          • Fallthrough
          • Func function definition
          • Defer delay
          • Panic recover Try Catch
          • Import operation
          • struct Custom type

Go language

Characteristics:

Minimalist syntax, native multithreading support;

Words of Praise:

Explanatory flexibility, dynamic type language efficiency, static type of security;

Chapter1:

Gopath is the working directory of the Go language;
* Basic FILE--$GOPATH/src, pkg, bin
* Go BUILD: Compile, if the main package generates the same executable file with the same directory name
* Go Install build Package
* Go FMT can automatically format code style;

Tips

    • Array_linux.go Array_darwin.go build changes to the same compilation as the current system environment;

chatper2--Basic Syntax

Go default UTF-8 encoding;

Special reserved Keywords:

Chan, defer, Fallthrough, map, range, select,

Tips

    1. Fmt. Printf does not wrap automatically;
    2. Python's print will be automatically wrapped;
    3. _, B = 34, 35; # Variables assigned to "_" are discarded
    4. Unused variables, compile errors (good coercion);
    5. Int32 and int 64 cannot be added or reduced (no cast);
    6. Support plural: var c complex64 = 5 + 5i;
    7. The string anti-quote "is raw_string, not escaped;
    8. Iota is reset to 0 each time the value is assigned;
    9. array fixed length;
    10. Slice has a cap concept; When the cap is 0, slice.append () will open up new memory;

Variable request

vartypevartype'1''2'var'1''2''1''2'

var variable type; variable declaration at the end;
The default value can not be declared when the type is provided;
does not even provide keyword Var
VAR1, Var2: = ' 1 ', ' 2 '
The ": =" assignment is equivalent to defining the variable, but only for local variables;
Global variables must be explicitly declared with Var;

var variable;
Const constants;

Built-in types:

    1. Bool:true, False
    2. int, uint; and various fixed length int32, Int64; (non-additive between various int)
    3. String: Double quotation mark "", "inverted quotation mark" enclosed; (no single quotation mark) immutable;
      Change string, must become byte array, modified, then New_str = string (Btye_array)
    4. Error: Wrong type
    5. Array:var arr [N]type;
    6. Slice: Dynamic array, var arr []type;
    7. Map: Python dict; Rating: = map[string]float32 {"C": 4.5, "GO": 5}

The memory allocation of the go language-a separate area for each data type (so no value can be assigned between different int)

Iota enumeration values

const (    iota    iota    iota    w)        # x:0, y:1, z:2, w:3constiota   # 此时iota重置为0, t:0

Convention:
Capital letters start with public; for example, Sqrt
Lowercase letters start with Private; calculate

Process Control:

If

Conditional left and right no brackets ()
The condition is a declared variable (local variable, only valid within the IF statement block)

For

for i=0; i < 1000; i++ {}for ; sum < 1000; {    sum += 10}可简写为:for sum < 1000; {   sum += 10}# 其实就是 while(囧)

Break, Goto can support tags;

Switch

The break action is added by default for each case (you don't have to write so many break at last)

Fallthrough

Force switch case to continue execution after matching, do not automatically break;

Func function definition

func funcName(input1 type1, input2 type2) (output1 type1, output2 type2) {    return value1, value2}

Feelings: Embarrassed! Why put the return value behind the parameter;
The biggest benefit of naming the return value is that the Func is visible and can return directly;

Defer (delayed)

Executes in reverse order at the end of the function; (Advanced post-exit)
Often used for resource closure;

Panic + recover = Try catch;

Each package recommends an INIT function (like the init. py function in the Python package)
The main function is only available in the main package;

Import Operation:

import (    "fmt"    "net/http"    _ "github.com/ziutek/mymysql/godrv")# f 此时为 fmt 的别名; . 为当前目录的意思,http包里函数可以直接使用;# 同python 中  from net.http import *# _ 不引入变量,仅执行 init 函数;

struct custom type;

typestruct {    string    int    int}typestruct {    Human    string}

The anonymous definition is equivalent to the inheritance of other languages; Student default contains all fields of human;

# method 只需要在函数前面增加一个 receiver 即可;funcfloat64 {   return c.radius * c.radius * math.Pi}

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.