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:
The end of the line is one of three right brackets:), ], };
The end of the line is an increment or decrement operator (, – );
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;
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);
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 数据类型 常量名称 = 值;
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 变量名称 数据类型 = 值;
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:
Concurrent programming
Interface-oriented programming
Function multiple return values
Delay Execution
Exception handling
Powerful and high-performance network programming
Memory management and recycling
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!