如何在多个编程语言间切换自如
目录
Attention
要自由切换编程语言,必然要通过实践,编程语言之海浩瀚无边,本文会随着作者的实践而不断更新,敬请期待。
基本思路
- 熟悉各语言基本语法,怎么定义变量、函数、控制流等,这里有语法简介、数据结构等章节。
 - 识别各语言的特性,为什么 Python 有元组等,也就是要知道语言的特性和高级用法等。
 
在熟悉基本语法时,要能搞清楚各编程语言怎么描述相同的功能的。 在识别语言特性时,要能搞清楚各编程语言处理方式上的设计哲学。
语法简介
| 语法 | Python | Go | 
|---|---|---|
| 变量声明 | 动态类型,无需声明 | 静态类型,必须声明 | 
| 类型转换 | int(), str(), float(), tuple(), list(), set(), dict() | type_name(exp), strconv.Atoi(), strconv.Itoa(), strconv.ParseFloat(), string(), value.(type), value.(T) | 
| 输入输出 | print(), input() | fmt.Println(), fmt.Scan() | 
| 代码块 | 缩进(空格/制表符) | {} 包裹 | 
| 循环 | for、while | 只有 for | 
| 函数 | def,支持默认参数 | func,无默认参数 | 
| 错误处理 | try-except | 返回 error + if err != nil | 
| 并发 | threading(GIL 限制) | goroutine + channel | 
| 面向对象 | 完整类继承 | struct + interface | 
| 包管理 | pip + import | go mod + import | 
内置函数
Python 的内置函数更丰富,适合快速开发;Go 的内置函数较少,但更专注于底层控制和性能优化。
Go
- 内存分配:new(), make()
 - 类型转换:int(), float64(), string()
 - 集合操作:len(), cap(), append()
 - 并发:go, chan, close()
 - 错误处理:panic(), recover()
 
Python
- 输入输出:print(), input()
 - 类型转换:int(), float(), str(), bool()
 - 数学运算:abs(), round(), min(), max(), sum()
 - 迭代与序列操作:len(), range(), sorted(), enumerate()
 - 对象操作:type(), isinstance(), dir()
 - 高阶函数:map(), filter(), zip()
 
控制流
循环
/* Go 只有 for 循环 */
for i, val := range arr {
  // ...
}
for i := 0; i < len(arr); i++ {
  // ...
}for i in range(10):
  pass
for i in range(0, 10, 1):
  pass
for val in arr:
  pass
for i, val in enumerate(arr):
  pass
else
  pass
while len(queue) > 0:
  pass
else
  pass
while flag: print(str(flag))数据结构
栈
stack := make([]string, 0, 10)
// 入栈
stack = append(stack, "A")
// 出栈
v := stack[len(stack)-1]
stack = stack[:len(stack)-1]
// 判断是否为空
isEmpty := len(stack) == 0L = []
L.append('D')  # 入栈
L.pop()  # 出栈
L[-1]  # peek队列
queue := make([]int, 0)
// 入队
queue = append(queue, 10)
// 出队
v := queue[0]
queue = queue[1:]
// 判断是否为空
isEmpty := len(queue) == 0L = []
L.append('D')  # 入队
L.pop(0)  # 出队
L[0]  # peekcollections.deque是个高效的双端队列,具有popleft()append()方法。
集合
集合(set)是一个无序的不重复元素序列。
如果从广义上来看的话,数组、字典、元组、栈、队列等都是集合。
Go 无原生 Set,通常用 map[T]bool 或 map[T]struct{} 模拟。一般还会封装成一个结构体。
// 初始化一个 set(用 map[string]bool 模拟)
set := make(map[string]bool)
// 添加元素
set["apple"] = true
set["banana"] = true
// 检查元素是否存在
if set["apple"] {
    fmt.Println("apple exists") // 输出: apple exists
}
if _, ok := set["apple"]; ok {
  // 当使用 map[string]struct{} 时更省内存(struct{} 是零大小类型)
    fmt.Println("apple exists") // 输出: apple exists
}
// 删除元素
delete(set, "banana")
// 遍历集合
for key := range set {
    fmt.Println(key) // 输出: apple
}set1 = {1, 2, 3, 4}            # 直接使用大括号创建集合; 创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典
set2 = set([4, 5, 6, 7])      # 使用 set() 函数从列表创建集合
set2.add(8)  # 将元素 x 添加到集合 s 中,如果元素已存在,则不进行任何操作
set2.update([9,10])  # 参数可以是列表,元组,字典等
set2.remove(8)  # 将元素 x 从集合 s 中移除,如果元素不存在,则会发生错误
set2.discard(8)  # 移除集合中的元素,且如果元素不存在,不会发生错误
set2.pop()  # 随机删除集合中的一个元素
set2.clear()  # 清空集合
x in s  # 判断元素 x 是否在集合 s 中,存在返回 True,不存在返回 False
len(s)  # 计算集合 s 元素个数字典
// 字典是引用类型,必须初始化后再使用,键必须是可比较的类型
var myMap map[string]int  // 此时为 nil
// myMap["key"] = 1  // 如果没有初始化会 panic
myMap = make(map[string]int)  // 使用 make,会根据存储的键值对数量动态调整其容量,也可指定一个初始容量,有助于减少容器扩容带来的内存分配和重新哈希次数
myMap = map[string]int{"one": 1}  // 字面量
// 添加/修改:
m["key"] = value
// 读取,键不存在返回value类型的零值
value := m["key"]
// 删除,键不存在则静默处
delete(m, "key")
// 检查键是否存在
if value, exists := myMap["key"]; exists {
    // 处理 value
}
// 遍历
for key, value := range myMap {
    // ...
}
// 并发安全方法 1:sync.RWMutex 读写操作都比较均衡
var m = make(map[string]int)
var mutex = &sync.RWMutex{}
// 写操作加锁
mutex.Lock()
m["key"] = 1
mutex.Unlock()
// 读操作加读锁
mutex.RLock()
value := m["key"]
mutex.RUnlock()
// 并发安全方法 2:sync.Map 读多写少(例如缓存)
// Store(存储)、Load(加载)、Delete(删除)、Range(遍历)
var m sync.Map  // 开箱即用,无需初始化(因为 sync.Map 的零值即未初始化的状态就是可用的,声明时在内部已经处理了初始化的逻辑,在 sync.Map 的内部实现中,如果底层的存储结构尚未初始化,会在第一次使用时进行初始化,这也是 Go 的一个设计哲学:类型的零值应该是立即可用的),其键和值类型都是interface{}
    
// 存储键值对
m.Store("name", "Alice")
m.Store("age", 25)
m.Store("city", "Beijing")
// 加载值
if value, ok := m.Load("name"); ok {
    fmt.Println("Name:", value) // Name: Alice
}
// 删除键
m.Delete("city")
// 检查键是否存在
if _, ok := m.Load("city"); !ok {
    fmt.Println("City key deleted")
}# 空字典,字典可以动态增长和收缩
empty_dict = {}
empty_dict2 = dict()
# 带初始值的字典,键必须是可哈希的(不可变类型),值可以是任意类型
person = {'name': 'Alice', 'age': 25, 'city': 'Beijing'}
person2 = dict(name='Bob', age=30, city='Shanghai')  # 关键字参数形式
# 从键值对序列创建
items = [('name', 'Charlie'), ('age', 35), ('city', 'Guangzhou')]
person3 = dict(items)
# 字典推导式
squares = {x: x*x for x in range(1, 6)}  # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
# 访问元素
print(person['name'])           # Alice
print(person.get('age'))        # 25
# 安全的访问方式
print(person.get('country'))    # None (不会报错,避免 KeyError)
print(person.get('country', 'China'))  # China (提供默认值)
# 修改和添加
person['age'] = 26              # 修改已有键
person['country'] = 'China'     # 添加新键
# 删除元素
del person['city']              # 删除键值对
age = person.pop('age')         # 删除并返回值
key, value = person.popitem()   # 删除并返回最后插入的项
# 清空字典
person.clear()
# 获取所有键
keys = person.keys()            # dict_keys(['name', 'age', 'city'])
print(list(keys))               # ['name', 'age', 'city']
# 获取所有值
values = person.values()        # dict_values(['Alice', 25, 'Beijing'])
# 获取所有键值对
items = person.items()          # dict_items([('name', 'Alice'), ('age', 25), ('city', 'Beijing')])
# 检查键是否存在
print('name' in person)         # True
# 字典长度
print(len(person))              # 3
# 合并字典方法 1:update 
person.update({'age': 26, 'city': 'Beijing'})
print(person)   # {'name': 'Alice', 'age': 26, 'country': 'China', 'city': 'Beijing'}
# 合并字典方法 2:解包 (Python 3.5+)
dict1 = {'a': 1, 'b': 2}
merged = {**dict1, **dict2}
print(merged)  # {'a': 1, 'b': 3, 'c': 4}
# 合并字典方法 3: | 运算符 (Python 3.9+)
merged = dict1 | dict2
print(merged)  # {'a': 1, 'b': 3, 'c': 4}
# 浅拷贝
original = {'a': 1, 'b': [2, 3]}
shallow_copy = original.copy()
# 深拷贝
import copy
deep_copy = copy.deepcopy(original)
deep_copy['b'].append(5)
print(original)  # {'a': 1, 'b': [2, 3, 4]} - 原字典不受影响
# 遍历键
for key in person:
    print(key)
for key in person.keys():
    print(key)
# 遍历值
for value in person.values():
    print(value)
# 遍历键值对
for key, value in person.items():
    print(f"{key}: {value}")
# 带索引的遍历
for i, (key, value) in enumerate(person.items()):
    print(f"{i}: {key} = {value}")
# 按键排序
sorted_by_key = dict(sorted(scores.items()))
# 按值排序
sorted_by_value = dict(sorted(scores.items(), key=lambda x: x[1]))
# 按值降序
sorted_desc = dict(sorted(scores.items(), key=lambda x: x[1], reverse=True))
# 在 asyncio 协程中,因为所有任务在单线程内通过事件循环调度,不存在真正的并行执行,所以在协程中操作字典是安全的,无需额外加锁
# 多线程安全需要上锁
# 多进程之间不共享内存,也无需考虑
import threading
my_lock = threading.Lock()
# 非线程安全的复合操作
if "key" not in my_dict:
    my_dict["key"] = value  # 在检查后、设置前,其他线程可能已经修改了字典
# 线程安全的复合操作
with my_lock:  # 假设 my_lock 是已经定义好的锁
    if "key" not in my_dict:
        my_dict["key"] = valuefrom collections import defaultdict, OrderedDict, Counter
# 默认字典,有默认值的字典
word_count = defaultdict(int)
word_count["hello"] += 1  # 自动初始化为0
# 计数器
counter = Counter("hello world")
print(counter)  # Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
# 有序字典(现在普通字典也有序了)
ordered = OrderedDict([('a', 1), ('b', 2), ('c', 3)])字符串
串是特殊的线性表,字符串的操作一般有:
- 字符串替换
 - 字符串查找
 - 字符串连接
 - 字符串分割
 - 大小写转换
 - 去除空格
 - 字符串判断
 - 字符串格式化
 - 字符串构建
 
两种语言的字符串都是不可变的。需要注意的是:
- Python3 的字符串是 Unicode 码点序列,Go 字符串是 UTF-8 字节序列(rune 才是 Unicode 码点)
 - Python 使用对象方法,而 Go 使用 strings 包函数
 - Go 需要显式错误处理,Python 使用异常
 - 索引访问时,Python 直接返回字符,Go 返回字节
 - Python支持负数索引,Go 不支持
 - Python没有单独的字符类型,字符串(str)是由单个字符组成的序列,也就是说,一个字符就是一个长度为1的字符串;而在 Go 中处理 ASCII 字符时常用 byte,而处理 Unicode 字符时常用 rune,按字符遍历,用 for range 循环时会将字符串解码为 rune。
 
// 字符串替换
s := "hello world"
newS := strings.Replace(s, "world", "golang", -1) // "hello golang"
newS2 := strings.Replace(s, "l", "L", 2)          // "heLLo world"
// 字符串判断
s := "hello"
starts := strings.HasPrefix(s, "he")  // true
ends := strings.HasSuffix(s, "lo")    // true
// Go标准库没有直接的isalpha/isdigit,需要自定义或使用unicode包
isAlpha := true
for _, r := range s {
    if !unicode.IsLetter(r) {
        isAlpha = false
        break
    }
}
// 字符串分割
strings.Split("a,b,c", ",") // ["a" "b" "c"], Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
strings.Split("a man a plan a canal panama", "a ") // "" "man " "plan " "canal panama"]
strings.Split(" xyz ", "")  // [" " "x" "y" "z" " "], If sep is empty, Split splits after each UTF-8 sequence.
strings.SplitN("a,b,c,d", ",", 3)  // []string{"a", "b", "c,d"}
// 字符串转整数
s := "123"
numInt, err := strconv.Atoi(s)           // 123
if err != nil {
    fmt.Println("转换错误:", err)
}
// 指定基数的转换
numHex, _ := strconv.ParseInt("ff", 16, 64)  // 255 - 十六进制
numBin, _ := strconv.ParseInt("1010", 2, 64) // 10 - 二进制
// 字符串转浮点数
sFloat := "3.14"
numFloat, err := strconv.ParseFloat(sFloat, 64) // 3.14
if err != nil {
    fmt.Println("转换错误:", err)
}
// 无符号整数转换
numUint, _ := strconv.ParseUint("123", 10, 64)
// 布尔值转换
boolVal, _ := strconv.ParseBool("true") // true
fmt.Println(numInt, numHex, numBin, numFloat, numUint, boolVal)
// 安全转换函数
func safeAtoi(s string) int {
    if num, err := strconv.Atoi(s); err == nil {
        return num
    }
    return 0
}
result := safeAtoi("123")  // 123
result2 := safeAtoi("abc") // 0# 字符串替换
s = "hello world"
new_s = s.replace("world", "golang")  # "hello golang"
new_s2 = s.replace("l", "L", 2)       # "heLLo world"
# 字符串判断,Python 中蛮多方法的
s = "hello"
starts = s.startswith("he")   # True
ends = s.endswith("lo")       # True
is_alpha = s.isalpha()        # True, Return True if all characters in the string are alphabetic and there is at least one character, False otherwise.
is_digit = s.isdigit()        # False, Return True if all characters in the string are digits and there is at least one character, False otherwise. # isdecimal() ⊆ isdigit() ⊆ isnumeric()
# 字符串分割
'1,2,3'.split(',')  # ['1', '2', '3'], Return a list of the words in the string, using sep as the delimiter string.
'1,2,3'.split(',', maxsplit=1)  # ['1', '2,3']
'1,2,,3,'.split(',')  # ['1', '2', '', '3', '']
'1 2 3'.split()  # ['1', '2', '3'] If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
'   1   2   3   '.split()  # ['1', '2', '3']
# 字符串转整数
s = "123"
num_int = int(s)           # 123
num_hex = int("ff", 16)    # 255 - 十六进制
num_bin = int("1010", 2)   # 10 - 二进制
# 字符串转浮点数
s_float = "3.14"
num_float = float(s_float) # 3.14
# 错误处理
try:
    num = int("abc")
except ValueError as e:
    print(f"转换错误: {e}")
# 安全转换(不会抛出异常)
def safe_int(s, default=0):
    try:
        return int(s)
    except ValueError:
        return default
result = safe_int("123")  # 123
result2 = safe_int("abc") # 0语言特性
类型
- 在 Python 中,空容器(如空列表、空字符串、空字典等)在布尔上下文中被视为 
False,而非空容器被视为True,所以可以使用while stack表示栈不为空时的循环。 - 圆括号 () → 元组,方括号 [] → 列表,花括号 {} → 集合或字典
 
函数
在 Java 中,如果在函数中修改传递给函数的引用所对应的对象,是值传递,但是传递的是引用的值,会影响原对象。通过引用操作对象,效率高但需要注意副作用。
在 Go 中,如果在函数中修改传递给函数的 slice 或 map 里的元素,也是值传递,但由于这个值包含指向底层数组的指针,实际上也是传递的引用,会影响原切片,但使用
append()可能不会影响原切片。Go 更明确地控制内存分配和修改行为。在 Python 中,如果在函数中修改传递给函数的可变对象(如列表),实际上传递的是对象的引用,跟 Java 类似,会直接影响原对象。通过引用操作对象,效率高但需要注意副作用。
计算
Java 中,
/的行为取决于操作数类型,整数 / 整数 → 整数除法(截断),浮点数 / 整数 → 浮点除法,在设计上更注重类型安全和性能。Go 中,
/的行为取决于操作数类型,整数 / 整数 → 整数除法(截断),浮点数 / 浮点数 → 浮点除法,浮点数 / float64(整数)→ 浮点除法,在设计上更注重类型安全和性能,同时“显式优于隐式”,Go 的类型系统更严格,相比 Java 没做整数到浮点数的自动类型提升。Python 中的除法运算符
/总是返回一个浮点数(即使两个操作数都是整数并且可以整除),如果需要执行整数除法(即向下取整),可以使用//运算符。Python 的设计哲学强调代码的清晰性和可读性,“让简单的事情简单,复杂的事情可能” ,/总是进行真除法(true division),返回数学上准确的结果。