Home > Backend Development > C#.Net Tutorial > What is the difference between c language and go language?

What is the difference between c language and go language?

青灯夜游
Release: 2023-01-03 09:27:38
Original
9544 people have browsed it

Differences: 1. The extensions of C language source files are ".h" and ".c", and the extensions of Go language source files are ".go". 2. In C language, code is managed through files, while in Go language, code is managed through packages. 3. There are a total of 32 keywords in C language and a total of 25 keywords in Go language.

What is the difference between c language and go language?

The operating environment of this tutorial: windows7 system, c99&&GO version 1.18, Dell G3 computer.

Source file comparison

  • C language source file
File extension Source type
.h Header file, storing code declaration
.c C language source file, storing code implementation
  • Go language source file
File extension Source type
.go Go language source file, storing code Implementation

Comparison of code management

  • Manage code through files in C language
    • When you want to use a certain function, you only need to include the corresponding .h file
  • Go language uses packages to manage code
    • Go language does not have .h The concept of files, when you want to use a certain function in Go, you only need to import the corresponding package
  • Public and private management of functions and variables in C language
    • Whether to expose functions and variables through extern and static
  • Public and private management of functions and variables in Go language
    • Whether to expose functions through the capitalization of the first letter of the function name
    • Realize whether to expose variables by case of the first letter of the variable name

Comparison of keywords

  • C language has a total of 32 keywords
##switchcasedefaultbreakreturnwhileforint##signedunsignedconstautoexternvolatileThere are a total of 25 in Go language Keywords
1 2 3 4 5 6 7 8
##if ##else ##goto ##do
##continue typedefstruct enumunion char short
#long float double void sizeof
registerstatic
    1
  • 2
345 678##elseswitch##continuetypeconstfuncinterfacepackagedefergo selectchanC language data type
##if
##case default ##break #return ##goto fallthrough for
struct var maprangeimport




Data type comparison



Go language data Type

  • What is the difference between c language and go language?Each data type in C language occupies memory space

  • TypeWhat is the difference between c language and go language?
    32-bit compiler

  • 64-bit compiler

char14448288##void*48
  • Each data type in Go language occupies memory space
1 int
float 4
double 8
short 2
long 4
long long 8
##int8/uint811signed char/unsigned char##int16/uint16int32/uint32int64/ uint64byteruneintuintptrfloat32float64truefalse
Type 32-bit compiler 64 Bit compiler essence
2 2 signed short/unsigned short
4 4 signed int/unsigned int
8 8 signed long long int/unsigned long long int
1 1 uint8/unsigned char
4 4 int32/signed int
4 8 The length is determined based on the number of machine digits
4 8 Determine the length uint32/uint64 according to the number of machine digits
4 4 float
8 8 double
1 1 Integer type of char type
1 1 char type integer
and C language Similarly, the Go language also provides Sizeof to calculate the memory space of variables
  • 1. Import the import "unsafe" package
    • 2. Calculate the variable memory space through unsafe.Sizeof()

Internal implementation of basic data types in Go language
  • golang official website download go1.4 version source code
    • The code of the older version The purer it is, the more suitable it is for novices to learn
      • As the code is updated and iterated, it will gradually become very complex, so it is recommended to download version 1.4
    • After decompression Open the path:
    • go\src\runtime\runtime.h

    What is the difference between c language and go language?
    ##Get the following implementation code

    // 第8行到35行
    typedef signed char     int8;
    typedef unsigned char       uint8;
    typedef signed short        int16;
    typedef unsigned short      uint16;
    typedef signed int      int32;
    typedef unsigned int        uint32;
    typedef signed long long int    int64;
    typedef unsigned long long int  uint64;
    typedef float           float32;
    typedef double          float64;
    
    #ifdef _64BIT
    typedef uint64      uintptr;
    typedef int64       intptr;
    typedef int64       intgo; // Go's int
    typedef uint64      uintgo; // Go's uint
    #else
    typedef uint32      uintptr;
    typedef int32       intptr;
    typedef int32       intgo; // Go's int
    typedef uint32      uintgo; // Go's uint
    #endif
    
    #ifdef _64BITREG
    typedef uint64      uintreg;
    #else
    typedef uint32      uintreg;
    #endif
    
    // 第153行到157行
    enum
    {
        true    = 1,
        false   = 0,
    };
    Copy after login
install B moment:The essence of Go is a high-level programming language written in C language
So Brother Jiang taught you C language earlier In order to allow you to understand the implementation code of Go today, you should know what it is and why it is done



Comparison of constant variables

C language defines constant and variable format

    数据类型 变量名称 = 值;
    const 数据类型 常量名称 = 值;
    Copy after login
  • Go language defines constant and variable format
      In addition to the following standard formats, Go language also provides several simple syntaxes Sugar
      var 变量名称 数据类型 = 值;
      const 变量名称 数据类型 = 值;
      Copy after login
    Comment comparison

    Like C language, Go language also supports single-line comments and multiple Line comments, and all comments have the same characteristics as C language

      Single-line comments
    • //Commented content
      • Multi-line comments/*Commented content* /
      • In the Go language, it is officially recommended to use single-line comments instead of multi-line comments (for details, you can directly view the Go official source code)
    Operator comparison

    The arithmetic operators are almost the same as those in C language

      In Go language, the -- operator does not support preposition
      • Incorrect way of writing: i; --i;
        • In Go language, -- is a statement, not an expression, so it must be on its own line
      • Incorrect way of writing : a = i ; return i ;
    #OperatorDescriptionAddSubtractMultiplyDivide##%Find the remainderB % AincrementA -- DecrementA--The relational operator is the same as C language
    Example
    A B -
    A - B *
    A * B /
    B / A

    • Operator
    DescriptionInstanceCheck Whether the two values ​​​​are equal, return True if they are equal, otherwise return False. A == BA != BA > BA ##>=Check whether the left value is greater than or equal to the right value, if so, return True, otherwise return False. A >= B
    ##====
    != Checks whether the two values ​​​​are not equal, returns True if they are not equal, otherwise returns False.
    > Check whether the left value is greater than the right value, if so, return True, otherwise return False.
    Check whether the left value is less than the right value, if so, return True, otherwise return False.
    ## Check whether the left value is less than or equal to the right value, if so, return True, otherwise return False. A

    • 逻辑运算符和C语言一样
    运算符 描述 实例
    && 如果两边的操作数都是 True,则条件 True,否则为 False。 A && B
    || 如果两边的操作数有一个 True,则条件 True,否则为 False。 A || B
    ! 如果条件为 True,则逻辑 NOT 条件 False,否则为 True。 !A

    • 位运算符和C语言几乎一样
      • 新增一个&^运算符
    运算符 描述 实例
    & 参与运算的两数各对应的二进位相与, 对应位只要都是1结果就为1 A & B
    | 参与运算的两数各对应的二进位相或,对应位只要其中一个是1结果就为1 A | B
    ^ 参与运算的两数各对应的二进位相异或,对应位只要不同结果就是1 A ^ B
    左移运算符,左移n位就是乘以2的n次方 A
    >> 右移运算符,右移n位就是除以2的n次方 B >> 2
    &^ 逻辑清零运算符, B对应位是1,A对应位清零,B对应位是0, A对应位保留原样 A &^ B
    int main(){
        /*
          0110      a
        &^1011      b 如果b位位1,那么结果为0, 否则结果为a位对应的值
        ----------
          0100
        */
        a1 := 6
        b1 := 11
        res1 := a1 &^ b1
        fmt.Println("res1 = ", res1) // 4
    
        /*
          1011      a
        &^1101      b 如果b位位1,那么结果为0, 否则结果为a位对应的值
        ----------
          0010
        */
        a2 := 11
        b2 := 13
        res2 := a2 &^ b2
        fmt.Println("res2 = ", res2) // 2
    }
    Copy after login

    • 赋值运算符和C语言几乎一样
      • 新增一个&^=运算符
    运算符 描述 实例
    = 将右边赋值给左边 C = A + B 将 A + B 表达式结果赋值给 C
    += 相加后再赋值 C += A 等于 C = C + A
    -= 相减后再赋值 C -= A 等于 C = C - A
    *= 相乘后再赋值 C *= A 等于 C = C * A
    /= 相除后再赋值 C /= A 等于 C = C / A
    %= 求余后再赋值 C %= A 等于 C = C % A
    左移赋值 C
    >>= 右移赋值 C >>= 2 等于 C = C >> 2
    &= 位逻辑与赋值 C &= 2 等于 C = C & 2
    ^= 位逻辑或赋值 C ^= 2 等于 C = C ^ 2
    |= 位逻辑异或赋值 C |= 2 等于 C = C | 2
    &^= 位逻辑清零赋值 C &^= 2 等于 C = C &^ 2

    流程控制语句对比

    • C语言流程控制中的if、switch、for在Go语言都可以使用
    • C语言中的四大跳转语句return、break、continue、goto在Go语言都可以使用
    • Go语言除了实现C语言中if、switch、for、return、break、continue、goto的基本功能以外,还对if、switch、for、break、continue进行了增强
      • 例如: if 条件表达式前面可以添加初始化表达式
      • 例如: break、continue可以指定标签
      • 例如: switch语句可以当做if/elseif来使用
      • ... ...
    • 值得注意的是Go语言中没有while循环和dowhile循环, 因为它们能做的Go语言中的for循环都可以做

    函数和方法对比

    • C语言定义函数格式
    返回值类型 函数名称(形参列表) {
            函数体相关语句;
            return 返回值;
    }
    Copy after login
    • Go语言定义函数格式
    func  函数名称(形参列表)(返回值列表) {
            函数体相关语句;
            return 返回值;
    }
    Copy after login
    • C语言中没有方法的概念, 但是Go语言中有方法
      • 对于初学者而言,可以简单的把方法理解为一种特殊的函数
    func  (接收者 接受者类型)函数名称(形参列表)(返回值列表) {
            函数体相关语句;
            return 返回值;
    }
    Copy after login

    编程思想对比

    • C语言是一门面向过程的编程语言
      • 面向过程: 按部就班, 亲力亲为,关注的是我应该怎么做?
      • 做饭例子: 面向过程做饭
        • 1.上街买菜
        • 2.摘菜
        • 3.洗菜
        • 4.切菜
        • 5.开火炒菜
        • 6.淘米煮饭
        • 7.吃饭
    • Go语言是门面向对象的编程语言
      • 面向对象:化繁为简, 能不自己干自己就不干,关注的是我应该让谁来做?
      • 做饭例子: 面向对象做饭
      • 1.找个会做饭女朋友 or 男朋友
      • 2.老婆我饿了 or 老公我饿了
      • 3.躺着...等她/他把饭做好
      • 4.吃饭
    • 不要把面向过程和面向对象想象得那么神奇, 它们只是思考问题的方式不同而已

    更多编程相关知识,请访问:编程视频!!

    The above is the detailed content of What is the difference between c language and go language?. For more information, please follow other related articles on the PHP Chinese website!

  • Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template