快速入门一种语言的办法是比较
我把java ,kotlin ,js , dart,python 易语言 go c 都写过, 甚至sql 表达编程
一天写4种语言。。 列如 java c# ,js , sql 语言,lua,
今天搞了下go 我觉得为了快速学习,我干脆整理了一个差异表格 实则所有语言都大差不差。
列如 sql server的 if begin end
和lua 的if then end
是不是又很像
编程语言语法和编译特性对比表(完整版)
基础语言信息
语言 |
类型 |
编程范式 |
首次发布 |
开发者 |
C++ |
编译 |
面向对象、过程式、泛型 |
1985 |
Bjarne Stroustrup |
C# |
编译/JIT |
面向对象、函数式 |
2000 |
Microsoft |
Java |
编译/JIT |
面向对象 |
1995 |
Oracle (Sun) |
JavaScript |
解释/JIT |
多范式 |
1995 |
Netscape |
PHP |
解释 |
面向对象、过程式 |
1995 |
Rasmus Lerdorf |
Swift |
编译 |
多范式 |
2014 |
Apple |
Kotlin |
编译/JIT |
多范式 |
2011 |
JetBrains |
Dart |
编译/JIT |
多范式 |
2011 |
Google |
Python |
解释 |
多范式 |
1991 |
Python基金会 |
Lua |
解释 |
多范式 |
1993 |
PUC-Rio |
Go |
编译 |
并发、过程式 |
2009 |
Google |
编译特性
语言 |
编译类型 |
目标平台 |
运行时 |
垃圾回收 |
C++ |
AOT编译 |
原生 |
原生 |
否(手动管理) |
C# |
字节码 → JIT |
.NET运行时 |
CLR |
是(分代GC) |
Java |
字节码 → JIT |
JVM |
JVM |
是(分代GC) |
JavaScript |
解释/JIT |
各种引擎 |
V8, SpiderMonkey等 |
是(标记清除) |
PHP |
解释 |
PHP引擎 |
Zend引擎 |
是(引用计数+循环GC) |
Swift |
AOT编译 |
原生/iOS/macOS |
原生/ARC |
ARC(自动引用计数) |
Kotlin |
字节码 → JIT |
JVM/原生/JS |
JVM/原生 |
是(在JVM上) |
Dart |
AOT/JIT |
原生/Dart VM |
Dart VM |
是(分代GC) |
Python |
字节码解释 |
Python VM |
CPython/PyPy |
是(引用计数+循环检测) |
Lua |
字节码解释 |
Lua VM |
Lua VM |
是(标记清除) |
Go |
AOT编译 |
原生 |
原生 |
是(三色标记) |
变量声明语法
语言 |
变量声明 |
类型注解 |
常量/不可变 |
C++ |
int x = 5; 或 auto x = 5;
|
int x |
const int X = 5; |
C# |
int x = 5; 或 var x = 5;
|
int x |
const int X = 5; |
Java |
int x = 5; 或 var x = 5; (Java 10+) |
int x |
final int X = 5; |
JavaScript |
let x = 5; 或 const x = 5;
|
TypeScript: let x: number
|
const x = 5; |
PHP |
$x = 5; |
PHP 7+: int $x = 5;
|
const X = 5; |
Swift |
var x = 5 或 let x = 5
|
var x: Int = 5 |
let x = 5 |
Kotlin |
var x = 5 或 val x = 5
|
var x: Int = 5 |
val x = 5 |
Dart |
var x = 5; 或 int x = 5;
|
int x = 5; |
final x = 5; |
Python |
x = 5 |
x: int = 5 (Python 3.6+) |
无内置常量 |
Lua |
local x = 5 |
无静态类型 |
无内置常量 |
Go |
var x int = 5 或 x := 5
|
var x int |
const X = 5 |
函数定义语法
语言 |
函数语法 |
返回类型 |
参数 |
C++ |
int add(int a, int b) { return a + b; } |
函数名前 |
(类型 名称) |
C# |
public int Add(int a, int b) { return a + b; } |
函数名前 |
(类型 名称) |
Java |
public int add(int a, int b) { return a + b; } |
函数名前 |
(类型 名称) |
JavaScript |
function add(a, b) { return a + b; } |
无显式类型 |
(名称) |
PHP |
function add($a, $b) { return $a + $b; } |
PHP 7+: : int
|
($变量名) |
Swift |
func add(_ a: Int, _ b: Int) -> Int { return a + b } |
-> 后 |
(标签 名称: 类型) |
Kotlin |
fun add(a: Int, b: Int): Int { return a + b } |
: 后 |
(名称: 类型) |
Dart |
int add(int a, int b) { return a + b; } |
函数名前 |
(类型 名称) |
Python |
def add(a, b): return a + b |
可选: -> int
|
(名称: 类型) |
Lua |
function add(a, b) return a + b end |
无显式类型 |
(名称) |
Go |
func add(a, b int) int { return a + b } |
参数后 |
(名称 类型) |
类定义语法
语言 |
类声明 |
构造函数 |
继承 |
C++ |
class MyClass { }; |
MyClass() { } |
: public BaseClass |
C# |
public class MyClass { } |
public MyClass() { } |
: BaseClass |
Java |
public class MyClass { } |
public MyClass() { } |
extends BaseClass |
JavaScript |
class MyClass { } |
constructor() { } |
extends BaseClass |
PHP |
class MyClass { } |
public function __construct() { } |
extends BaseClass |
Swift |
class MyClass { } |
init() { } |
: BaseClass |
Kotlin |
class MyClass { } |
constructor() { } |
: BaseClass() |
Dart |
class MyClass { } |
MyClass() { } |
extends BaseClass |
Python |
class MyClass: |
def __init__(self): |
class MyClass(BaseClass): |
Lua |
无内置类 |
使用表/元表 |
手动实现 |
Go |
type MyStruct struct { } |
无构造函数 |
组合,无继承 |
控制流语法
语言 |
if语句 |
for循环 |
while循环 |
C++ |
if (condition) { } |
for (int i = 0; i < 10; i++) { } |
while (condition) { } |
C# |
if (condition) { } |
for (int i = 0; i < 10; i++) { } |
while (condition) { } |
Java |
if (condition) { } |
for (int i = 0; i < 10; i++) { } |
while (condition) { } |
JavaScript |
if (condition) { } |
for (let i = 0; i < 10; i++) { } |
while (condition) { } |
PHP |
if ($condition) { } |
for ($i = 0; $i < 10; $i++) { } |
while ($condition) { } |
Swift |
if condition { } |
for i in 0..<10 { } |
while condition { } |
Kotlin |
if (condition) { } |
for (i in 0..9) { } |
while (condition) { } |
Dart |
if (condition) { } |
for (int i = 0; i < 10; i++) { } |
while (condition) { } |
Python |
if condition: |
for i in range(10): |
while condition: |
Lua |
if condition then end |
for i = 1, 10 do end |
while condition do end |
Go |
if condition { } |
for i := 0; i < 10; i++ { } |
for condition { } |
数组/列表语法
语言 |
数组声明 |
访问元素 |
长度/大小 |
C++ |
int arr[3] = {1, 2, 3}; |
arr[0] |
sizeof(arr)/sizeof(arr[0]) |
C# |
int[] arr = {1, 2, 3}; |
arr[0] |
arr.Length |
Java |
int[] arr = {1, 2, 3}; |
arr[0] |
arr.length |
JavaScript |
let arr = [1, 2, 3]; |
arr[0] |
arr.length |
PHP |
$arr = [1, 2, 3]; |
$arr[0] |
count($arr) |
Swift |
var arr = [1, 2, 3] |
arr[0] |
arr.count |
Kotlin |
val arr = arrayOf(1, 2, 3) |
arr[0] |
arr.size |
Dart |
var arr = [1, 2, 3]; |
arr[0] |
arr.length |
Python |
arr = [1, 2, 3] |
arr[0] |
len(arr) |
Lua |
local arr = {1, 2, 3} |
arr[1] (从1开始) |
#arr |
Go |
arr := [3]int{1, 2, 3} |
arr[0] |
len(arr) |
字符串操作
语言 |
字符串声明 |
连接 |
插值 |
C++ |
std::string str = "Hello"; |
str1 + str2 |
无内置(C++20 format) |
C# |
string str = "Hello"; |
str1 + str2 |
$"Hello {name}" |
Java |
String str = "Hello"; |
str1 + str2 |
String.format("Hello %s", name) |
JavaScript |
let str = "Hello"; |
str1 + str2 |
Hello ${name}“ |
PHP |
$str = "Hello"; |
$str1 . $str2 |
"Hello $name" |
Swift |
var str = "Hello" |
str1 + str2 |
"Hello (name)" |
Kotlin |
val str = "Hello" |
str1 + str2 |
"Hello $name" |
Dart |
var str = "Hello"; |
str1 + str2 |
"Hello $name" |
Python |
str = "Hello" |
str1 + str2 |
f"Hello {name}" |
Lua |
local str = "Hello" |
str1 .. str2 |
无内置 |
Go |
str := "Hello" |
str1 + str2 |
fmt.Sprintf("Hello %s", name) |
空值安全
语言 |
空值表明 |
空值安全操作符 |
空值检查 |
C++ |
nullptr |
无 |
if (ptr != nullptr) |
C# |
null |
?. ??
|
if (obj != null) |
Java |
null |
无(Java 14前) |
if (obj != null) |
JavaScript |
null / undefined
|
?. ??
|
if (obj != null) |
PHP |
null |
?? |
if ($obj !== null) |
Swift |
nil |
? ! ??
|
if let / guard let
|
Kotlin |
null |
?. !! ?:
|
if (obj != null) |
Dart |
null |
?. ?? !
|
if (obj != null) |
Python |
None |
无 |
if obj is not None: |
Lua |
nil |
无 |
if obj ~= nil |
Go |
nil |
无 |
if obj != nil |
内存管理
语言 |
内存模型 |
垃圾回收 |
手动内存管理 |
C++ |
手动管理 |
否 |
是(new/delete,智能指针) |
C# |
托管 |
是(分代GC) |
有限(不安全代码) |
Java |
托管 |
是(多种GC算法) |
否 |
JavaScript |
托管 |
是(V8使用标记清除) |
否 |
PHP |
托管 |
是(引用计数+循环GC) |
否 |
Swift |
ARC |
ARC(自动引用计数) |
手动(C互操作) |
Kotlin |
托管(JVM) |
是(在JVM上) |
否(JVM),手动(原生) |
Dart |
托管 |
是(分代GC) |
否 |
Python |
托管 |
是(引用计数+循环检测) |
否 |
Lua |
托管 |
是(标记清除) |
否 |
Go |
托管 |
是(三色标记) |
否 |
性能特性
语言 |
启动时间 |
运行时性能 |
内存使用 |
并发模型 |
C++ |
极快 |
极高 |
极低 |
线程、OpenMP |
C# |
中等(JIT) |
高 |
中等 |
线程 + async/await |
Java |
中等(JIT) |
高 |
中-高 |
线程 + 并发集合 |
JavaScript |
快 |
中-高(JIT) |
中等 |
事件循环 + async/await |
PHP |
快 |
低-中 |
中等 |
进程模型 + 多线程 |
Swift |
快 |
高 |
低-中 |
GCD + async/await |
Kotlin |
中等(JIT) |
高 |
中等 |
协程 + 线程 |
Dart |
快 |
中-高 |
中等 |
隔离 + async/await |
Python |
快 |
低-中 |
高 |
GIL + asyncio |
Lua |
快 |
中等 |
低 |
协程 |
Go |
快 |
高 |
低-中 |
Goroutines + Channels |
生态系统和用途
语言 |
主要用途 |
包管理器 |
IDE支持 |
社区规模 |
C++ |
系统编程、游戏、高性能 |
Conan、vcpkg |
Visual Studio、CLion |
超级大 |
C# |
桌面、Web、游戏 |
NuGet |
Visual Studio、VS Code |
大型 |
Java |
企业级、Android、Web |
Maven、Gradle |
IntelliJ、Eclipse |
超级大 |
JavaScript |
Web、Node.js、移动端 |
npm、yarn |
VS Code、WebStorm |
超级大 |
PHP |
Web开发、服务器端 |
Composer |
PHPStorm、VS Code |
大型 |
Swift |
iOS、macOS、服务器 |
Swift Package Manager |
Xcode、VS Code |
大型 |
Kotlin |
Android、JVM、多平台 |
Gradle、Maven |
IntelliJ、Android Studio |
增长中 |
Dart |
Flutter、Web、服务器 |
pub |
VS Code、IntelliJ |
增长中 |
Python |
数据科学、Web、自动化 |
pip、conda |
PyCharm、VS Code |
超级大 |
Lua |
嵌入式、游戏、配置 |
LuaRocks |
各种 |
中等 |
Go |
微服务、云原生、系统编程 |
go mod |
VS Code、GoLand |
大型 |
特殊语言特性
语言 |
独特特性 |
错误处理 |
并发原语 |
C++ |
模板、RAII、操作符重载 |
异常、错误码 |
std::thread、std::async |
C# |
LINQ、属性、事件 |
异常 |
Task、async/await |
Java |
注解、反射、JVM生态 |
异常 |
Thread、ExecutorService |
JavaScript |
原型继承、闭包、动态 |
异常、Promise |
Promise、async/await |
PHP |
弱类型、$变量、Web优化 |
异常、错误处理 |
进程、pthreads |
Swift |
可选类型、协议、值类型 |
异常、Result |
GCD、async/await |
Kotlin |
扩展函数、协程、空安全 |
异常 |
协程、Channel |
Dart |
隔离、mixin、异步 |
异常、Future |
Isolate、async/await |
Python |
装饰器、生成器、鸭子类型 |
异常 |
asyncio、threading |
Lua |
元表、协程、轻量级 |
错误函数 |
协程 |
Go |
接口、组合、简洁 |
错误值 |
Goroutine、Channel |
学习难度和适用场景
语言 |
学习难度 |
适合初学者 |
企业应用 |
个人项目 |
性能要求 |
C++ |
很高 |
否 |
高性能系统 |
系统工具 |
极高 |
C# |
中等 |
是 |
企业应用 |
桌面应用 |
高 |
Java |
中等 |
是 |
企业级系统 |
Android应用 |
高 |
JavaScript |
低-中 |
是 |
Web应用 |
前端项目 |
中 |
PHP |
低 |
是 |
Web开发 |
个人网站 |
中 |
Swift |
中等 |
是 |
iOS应用 |
移动应用 |
高 |
Kotlin |
中等 |
是 |
Android开发 |
跨平台应用 |
高 |
Dart |
低-中 |
是 |
Flutter应用 |
移动应用 |
中-高 |
Python |
低 |
是 |
数据科学 |
脚本工具 |
中 |
Lua |
低 |
是 |
嵌入式脚本 |
游戏脚本 |
中 |
Go |
低-中 |
是 |
微服务 |
系统工具 |
高 |
语法概念 |
Go 示例 |
C# 示例 |
Java 示例 |
包声明/入口 |
package main |
namespace MyApp { class Program { ... }} |
package myapp; |
主函数入口 |
func main() {} |
static void Main(string[] args) |
public static void main(String[] args) |
控制台输出 |
fmt.Println("Hello") |
Console.WriteLine("Hello"); |
System.out.println("Hello"); |
变量声明 |
var x int = 10 |
int x = 10; |
int x = 10; |
自动类型推导 |
x := 10 |
var x = 10; |
✘ 不支持自动推导(Java 10 之后支持 var ) |
常量声明 |
const Pi = 3.14 |
const double Pi = 3.14; |
final double Pi = 3.14; |
数组 |
arr := [3]int{1, 2, 3} |
int[] arr = new int[] {1,2,3}; |
int[] arr = {1, 2, 3}; |
动态数组(切片) |
s := []int{1, 2, 3} |
List<int> s = new List<int> {1,2,3}; |
ArrayList<Integer> s = new ArrayList<>(); |
字典/Map |
m := map[string]int{"a": 1} |
var m = new Dictionary<string, int>() |
Map<String, Integer> m = new HashMap<>(); |
if 条件语句 |
if x > 0 { ... } |
if (x > 0) { ... } |
if (x > 0) { ... } |
for 循环 |
for i := 0; i < 10; i++ {} |
for (int i = 0; i < 10; i++) {} |
for (int i = 0; i < 10; i++) {} |
foreach 循环 |
for _, v := range arr {} |
foreach (var v in arr) {} |
for (int v : arr) {} |
switch 语句 |
switch x { case 1: ... } |
switch (x) { case 1: ... break; } |
switch (x) { case 1: ... break; } |
函数定义 |
func add(a, b int) int { return a + b } |
int Add(int a, int b) => a + b; |
int add(int a, int b) { return a + b; } |
多值返回 |
return a, b |
(out a, out b) |
✘ 不支持(需封装类或使用数组) |
类/结构体定义 |
type Person struct { Name string } |
class Person { public string Name; } |
class Person { public String name; } |
方法 |
func (p Person) Greet() { ... } |
public void Greet() {} |
public void greet() {} |
接口定义 |
type Speaker interface { Speak() } |
interface ISpeaker { void Speak(); } |
interface Speaker { void speak(); } |
实现接口 |
func (d Dog) Speak() {} |
class Dog : ISpeaker { ... } |
class Dog implements Speaker { ... } |
指针 |
var p *int = &x |
✘ 不支持裸指针(通过 ref/out 模拟) |
✘ 不支持裸指针 |
错误处理 |
if err != nil { ... } |
try { ... } catch (Exception ex) |
try { ... } catch (Exception e) |
并发 |
go func() {} (使用 goroutine) |
Task.Run(() => { ... }) 或 Thread
|
new Thread(() -> { ... }).start(); |
概念 |
Go 示例 |
Python 示例 |
Java 示例 |
C 示例 |
JavaScript 示例 |
变量声明 |
var x int = 10 |
x = 10 |
int x = 10; |
int x = 10; |
let x = 10; |
自动类型推导 |
x := 10 |
x = 10 |
✘ 不支持自动推导 |
✘ 不支持 |
let x = 10; |
常量 |
const Pi = 3.14 |
PI = 3.14 (惯例大写) |
final double PI = 3.14; |
#define PI 3.14 |
const PI = 3.14; |
函数定义 |
func add(a, b int) int { return a + b } |
def add(a, b): return a + b |
int add(int a, int b) { return a + b; } |
int add(int a, int b) { return a + b; } |
function add(a, b) { return a + b; } |
主函数入口 |
func main() {} |
if __name__ == "__main__": |
public static void main(String[] args) |
int main() {} |
window.onload = function() {} |
包导入 |
import "fmt" |
import math |
import java.util.*; |
#include <stdio.h> |
import * as fs from fs ; |
条件语句 |
if x > 0 {} |
if x > 0: |
if (x > 0) {} |
if (x > 0) {} |
if (x > 0) {} |
循环(for) |
for i := 0; i < 10; i++ {} |
for i in range(10): |
for (int i = 0; i < 10; i++) |
for (int i = 0; i < 10; i++) |
for (let i = 0; i < 10; i++) |
switch-case |
switch x { case 1: ... } |
match x: (Python 3.10+) |
switch(x) { case 1: ... } |
switch(x) { case 1: ... } |
switch(x) { case 1: ... } |
数组 |
arr := [3]int{1, 2, 3} |
arr = [1, 2, 3] |
int[] arr = {1, 2, 3}; |
int arr[] = {1, 2, 3}; |
let arr = [1, 2, 3]; |
切片(动态数组) |
s := []int{1, 2, 3} |
s = [1, 2, 3] |
ArrayList<Integer> s = new ArrayList<>(); |
✘ 手动实现动态数组 |
let s = [1, 2, 3]; |
map / 字典 |
m := map[string]int{"a": 1} |
m = {"a": 1} |
Map<String, Integer> m = new HashMap<>(); |
✘ 需结构体模拟 |
let m = {"a": 1}; |
指针 |
var p *int = &x |
✘ 不支持 |
✘ 不常用(封装) |
int *p = &x; |
✘ 不支持 |
并发 |
go func() {} |
asyncio / threading
|
Thread t = new Thread(...); |
pthread_create() |
Promise , async/await
|
错误处理 |
if err != nil { ... } |
try: ... except |
try { ... } catch(Exception e) |
if (err) { ... } |
try { ... } catch (e) {} |
语言 |
方式一:使用运算符 +
|
方式二:格式化函数/方法 |
方式三:模板/插值语法 |
Go |
"Hello, " + name |
fmt.Sprintf("Hello, %s", name) |
✘ 不支持原生插值(只能用 Sprintf) |
C# |
"Hello, " + name |
string.Format("Hello, {0}", name) |
$"Hello, {name}" (推荐 ✅) |
Java |
"Hello, " + name |
String.format("Hello, %s", name) |
String s = STR."Hello, {name}"; (Java 21+) |
Python |
"Hello, " + name |
"Hello, {}".format(name) |
f"Hello, {name}" (推荐 ✅) |
JS |
"Hello, " + name |
不常用:类似 format 函数需自定义 |
`Hello, ${name}` (推荐 ✅) |
概念 |
Go 示例 |
Lua 示例 |
Java 示例 |
Swift 示例 |
Kotlin 示例 |
变量声明 |
var x int = 10 |
local x = 10 |
int x = 10; |
var x: Int = 10 |
var x: Int = 10 |
自动类型推导 |
x := 10 |
local x = 10 |
✘ Java 10+ 可用 var x = 10;
|
var x = 10 |
val x = 10 |
常量 |
const Pi = 3.14 |
local PI = 3.14 (没有真正常量) |
final double PI = 3.14; |
let pi = 3.14 |
const val PI = 3.14 |
函数定义 |
func add(a, b int) int { return a + b } |
function add(a, b) return a + b end |
int add(int a, int b) { return a + b; } |
func add(a: Int, b: Int) -> Int |
fun add(a: Int, b: Int): Int = a + b |
主函数入口 |
func main() {} |
无强制入口点 |
public static void main(...) |
@main struct App { static func main() } |
fun main() {} |
条件语句 |
if x > 0 {} |
if x > 0 then ... end |
if (x > 0) {} |
if x > 0 {} |
if (x > 0) {} |
循环 |
for i := 0; i < 10; i++ {} |
for i = 1, 10 do ... end |
for (int i = 0; i < 10; i++) {} |
for i in 0..<10 {} |
for (i in 0 until 10) {} |
数组/列表 |
[]int{1,2,3} |
{1,2,3} |
int[] arr = {1,2,3}; |
[1, 2, 3] |
listOf(1,2,3) |
字典/map |
map[string]int{"a":1} |
{a=1} |
Map<String, Integer> m = new HashMap<>(); |
["a": 1] |
mapOf("a" to 1) |
面向对象 |
type Person struct {...} + 方法 |
table + metatable 模拟 |
class Person {} |
class Person {} |
class Person {} |
并发支持 |
go func() {} (goroutine) |
✘ 无原生并发 |
Thread , ExecutorService
|
DispatchQueue , Task
|
coroutines , launch {}
|