Learning Go by Comparing to Python
本文为 Python 使用者提供了一份与 Go 对比的 Cheat sheet。
fmt.Println
v/sprint
fmt.Print
v/sprint(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/ssome_numbers = numbers[1:3]
- append:
append(scores, 1.1)
v/sscores.append(1.1)
- map initialize:
elements := make(map[string]int)
v/selements = {}
- map setter:
elements["H"] = 1
v/selements["H"] = 1
- map deletion:
delete(elements, "O")
v/selements.pop("O")
- map hasattr:
if number, ok := elements["O"]; ok {
v/sif "O" in elements:
- boolean:
if x != 0 {
v/sif not x:
&&
,||
,!
v/sand
,or
,not
- function:
func average(numbers ...float64) float64 {
v/sdef average(*numbers):
- defer:
defer f.Close()
v/stry: ... finally: f.close()
- struct v/s class
- ...