A comparative study of Go language and C language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.  Directory (?) [-]
    1. Key words
    2. Operator
    3. Variable
    4. Pointer
    5. Function
      1. return value
      2. Closed Package
    6. PO
    7. Programming specifications
    8. Oo
    9. Concurrent
    10. Project Management

For the markdown compiler does not have the automatic saving function this thing, I express serious disgust. One hours of tidying up to naught, and incredibly can not be imported into the HTML editor, really unbearable!

Key words

category C Go
Character Char No
String No String
Floating point number Double, float float32, Float64, complex64, complex128
Integer int long Short Uint8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64, uint, int, uintptr, byte = Uint8, Rune=unit32
bool No Boolean true, False
Function No Func
Interface No Interface
Empty void No
Any No interface{}
Polymerization Enum, struct, union struct
Symbol Qualification Symbol Signed, unsigned, auto, const, extern, register, Static, volatile Auto, const
Process Control Break, case, does, for, Goto, if, else, continue, default, return, switch, while For, if, else, goto, break, switch, case, default, Fallthrough, range, select, Range
Data No Map
Other sizeof, typedef Iota, Var, type, defer, go Chan, package, import

Operator

function C Go
Take counter ~ ^
Choose ? :
Fetching pointer content ->
Write read-out <- ->
Initialize and assign values :=

The rest of the operators are basically the same

Variable

X C Go
Declaration/definition TYPE NAMEint i (Var) NAME TYPEvar i int
Character (String) char , char [] byte byte[], string
Shaping bit width relies on specific platforms and compilers You can specify a width, or you can rely on the platform compiler
Valid range The variables opened up on the stack are valid in scope, and the variables that are opened on the heap are manually released before The last reference is valid before it leaves the scope
Scope control Static limit is valid for this file, extern looks for global variables for other files Uppercase start can be exported, lowercase only for this package use

Pointer

The Assumption a is a pointer to a struct in which a member is called b .

C Go
a->b a.b
    • The pointer & operator * and the pointer operator are the same.
    • In the pointer comparison, nil in Go is equivalent to the C language of NULL.

Function

return value

C Go
There can only be one can have multiple

Like what:

func exchange( a  int , b int ) ( int , int ){ return b , a}
    • 1
    • 2
    • 3
    • 4

Closed Package

C Go
No Yes

C-language functions and variables are two different concepts, and variables can only be identified by a function pointer in the form of a function.
The function of the Go language is also a variable.
Try the following code:

Package MainImport "FMT" func getfunc () func (int) int {i: = 0 //this variable is bound to the following return function return increment_i (J int) int {i + = j; return I}} func Main () {func_i1: = Getfunc () Func_i2: = Getfunc () // Above two call Getfunc, get two increment_i functions, each binding their own I variable for I: =0; I < 10; i++ {fmt. Println ( "FUNC1 print:", FUNC_I1 (i)) fmt. Println ( "FUNC2 print:", Func_i2 (I+1))}}    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

PO

X C Go
If "if (condition) xxx else xxx" ' "' If condition {xxx} else {xxx} '
Switch Can only choose to be converted to reshape, case without break automatically continue Can be of any type and will not automatically continue unless Failthrough is used
Select It's a function. keyword, select chan
For for (;;) Xxx for; {XXX}
While Yes No
Do ... while Yes No

Programming specifications

    • No semicolon required at the end of sentence
    • The If for code snippet must be surrounded by curly braces
    • If the condition not parentheses
    • The for indication section does not have parentheses
    • The left curly brace cannot be self-line

Oo

    • Method
      Between the keyword Func and the function name the description belongs to that struct.
struct {    a int}func ( this * OOTest) GetAByPointer() int {    return this.a}func ( this OOTest) GetAByCopy() int { return this.a}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • Interface
      defining interfaces, explaining function types
type IOOTest interface {    GetAByPointer() int }
    • 1
    • 2
    • 3

The interface object is assigned to the struct object pointer that implements the corresponding interface when used.

var i_test IOOTestvar test OOTest i_test = &test
    • 1
    • 2
    • 3
    • Combination
      There's nothing to say.

    • Inherit (by anonymous combination)

type OOTest1 struct{    OOTest b int}
    • 1
    • 2
    • 3
    • 4

This OOTest1 will have OOTest the member variables and methods

Concurrent

X C + + Go
Realize Threads that depend on the system interface Go keyword, goroutine co-process
Lock Rely on system supply or library or own implementation Sync Pack
Communication Shared Memory/Message Message chan mechanism

Project Management

X C + + Go
Compiler Self-selection Comes with
Engineering Management Self-selection Comes with
Testing mechanism Self-selection Comes with
Unified encoding Format Reliance on programmer Awareness Tools for Unified Finishing

Original address

http://studygolang.com/wr?u=http%3a%2f%2fblog.csdn.net%2fcchd0001%2farticle%2fdetails%2f44982127

Related Article

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.