What is the difference between golang and c?

青灯夜游
Release: 2020-04-17 16:39:09
Original
4575 people have browsed it

What is the difference between golang and c?

The difference between golang and c language

1. Surface difference - semicolon at the end of the line

Programmers who come from C, C, C#, and Java will be surprised that the Go code they see does not have an end-of-line semicolon (";") as a line terminator. Before explaining this problem, we declare an important fact: the formal Go syntax uses an English semicolon (";") as the ending identifier.

The reason why we don’t need to add a semicolon at the end of the line is that Go’s design is based on the following two considerations:

  • A few simple rules can be used to determine the end of a statement line. Therefore, the semicolon at the end of the line can be omitted;

  • Omitting the semicolon at the end of the line can better standardize the code.

When Golang compiles, when the last segment of a code line (non-comment line) is one of the following five situations, a semicolon is automatically inserted at the end of the line:

  1. The end of the line is one of three right brackets:), ], };

  2. The end of the line is an increment or decrement operator (, – );

  3. The end of the line is a specific value of type: Boolean value, integer, floating point number, complex number, Unicode code point or string literal;

  4. The end of the line is an identifier: including type name, variable name, constant name, jump label, package name and package introduction name (excluding keywords);

  5. line The end is 4 jump keywords: including break, continue, fallthrough and return (excluding other keywords).

2. 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, the Go language also provides several simple syntactic sugars

var 变量名称 数据类型 = 值;
const 变量名称 数据类型 = 值;
Copy after login

3. Code management comparison

C language Code is managed through files in Go

●When you want to use a certain function, you only need to include the corresponding .h file

Manage code through packages in Go language

●●Go language does not have the concept of .h 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

●● Whether to expose variables by capitalizing the first letter of the variable name

4. Core differences

summarizes the following eight points:

  1. Concurrent programming

  2. Interface-oriented programming

  3. Function multiple return values

  4. Delay Execution

  5. Exception handling

  6. Powerful and high-performance network programming

  7. Memory management and recycling

  8. Code cross-platform and cross-compilation

It is because of the above eight core differences that Go is known as the C language in the Internet era.

5. Specific differences

As for the specific differences between C and Go, there is a detailed comparison table on hyperpolyglot: http://hyperpolyglot.org/c

hyperpolyglot.org makes a detailed comparison of some similar development languages ​​and tools. You can take a look when you have nothing to do.

Recommended learning: Golang tutorial

The above is the detailed content of What is the difference between golang and c?. 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