Learning Go by Comparing to Python

查看原文

本文为 Python 使用者提供了一份与 Go 对比的 Cheat sheet。

  • fmt.Println v/s print
  • fmt.Print v/s print(string, end='')
  • //, /* ... */ v/s #, """ ... """
`This is 
a multi-line string.`

v/s

"""This is
a multi-line string.
"""
  • vector/list initialize: var numbers [5]int v/s [0] * 5
  • slice: some_numbers := numbers[1:3] v/s some_numbers = numbers[1:3]
  • append: append(scores, 1.1) v/s scores.append(1.1)
  • map initialize: elements := make(map[string]int) v/s elements = {}
  • map setter: elements["H"] = 1 v/s elements["H"] = 1
  • map deletion: delete(elements, "O") v/s elements.pop("O")
  • map hasattr: if number, ok := elements["O"]; ok { v/s if "O" in elements:
  • boolean: if x != 0 { v/s if not x:
  • &&, ||, ! v/s and, or, not
  • function: func average(numbers ...float64) float64 { v/s def average(*numbers):
  • defer: defer f.Close() v/s try: ... finally: f.close()
  • struct v/s class
  • ...