Brief summary of Golang first knowledge

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

1 Why Learn

Job needs, future investments


2 Features of the Golang

Set to server-side language, concise, to replace the C language.


3 Interesting features

Variable parameters of the function "1"

"2" The first letter of the variable function capital is public, lowercase is private;

"3" Chinese named variables and functions

"4" deferred execution, leaving scope after execution. Keyword defer (no more worrying about forgetting to release the statement) (The return value can be modified again when the function returns)

The forced cloud bracket formatting of the 5 function must be on the right side of the sentence of the function name. Not so also compile error.


4 Common language Features

Automatic garbage collection
Richer built-in types
function multiple return value
Error handling
Anonymous functions and closures
Types and Interfaces
Concurrent programming
Reflection
Language interactivity


Comparison of 5 and C

(# #总的说, the parentheses are omitted than the C language, the semicolon; the type name is followed by;)

1 header file references #include <***> vs import "FMT"

2 function Variable type definition

int mytype::funcname (int a, int b, string c)
{
function body
Return r//only single return value
}

Vs


Func (P myType) FuncName (A, B int, c string) (R, s int) {
function body
Return statement
}
Func: Keyword
(P MyType): Indicates the type object to which the function belongs! , that is, to define a method for a particular type, you can omit the non-write, that is, the normal function (here we mainly explain the normal function)
Name of function:
Parameters: (can not be declared)
Return value: (Can not be declared)
function Body:

3 Assigning values to variables

int a = 3; vs var a int = 3 or a:= 3 (can only be written in the body of the function, automatic type deduction)


4 For comparison

int i;
for (i =0; i<10; i++)
{
}

Vs


For i:=0; i<10; i++ {
}

"Note foreach"

var pow = []int{1, 2, 4, 8, 16, 32, 64, 128}

For I, V: = Range Pow {
Fmt. Printf ("2**%d =%d\n", I, v)
}

_ As a dummy, occupy a position with

For _, Value: = Range POW {
Fmt. Printf ("%d\n", value)
}

Comparison of 5 while

while (I<10)
{
}


Vs


For i<10 {
}

"Note" The Dead loop is

for {
}


6 If comparison

if (x > 0)
{
}


Vs


If x>0{
}

The note if statement can execute a simple statement before the condition. if V: = Math. Pow (x, N); V < Lim {


7 If Else comparison

if (T.hour () <12)
{
j = 2;
}
else if (T.hour () < 17)
{
j = 3;
}
Else
{
J=4
}


Vs


Switch {
Case T.hour () < 12:
j = 2
Case T.hour () < 17:
j = 3
Default
j = 4
}


8 Switch comparison

Switch (x)
{
Case 1:
Case 2:
xxx
i = 3
Break
Case 3:
j = 3
Break
Default
Break
}

Vs

Switch x{
Case 1:
Fallthrough
Case 2:
i = 3
Case 3:
j = 3
Default
Break
}


9 Dynamic array vector vs Slice


10



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.