编程语言比较学习

基础语法

Golang

  1. pkg and import
  2. exported Names and Functions
  3. multiple result
  4. names return values, they are treated as variables defined at the top of the functions

Python

变量

Golang

  1. var, at function and pkg level
  2. initialize(可省略类型)
  3. 函数外的语句必须以关键字开头

Python

基本数据类型(basic type)

array,list,map...

Golang

  1. bool
  2. string
  3. int
    1. int int8 int16 int32 int64
    2. unint unint8 unint16 unint32 unint64
  4. rune(int32):unicode code point
  5. float32 float64
  6. complex32 complex64
  7. zero value or initial value
    1. int:0
    2. bool:false
    3. string:""
  8. type conversion(类型转换)
  9. type interface(类型推导)

Python

高级数据类型

Golang

pointer

  1. pointer zero value is nil
  2. Go中没有指针运算

struct

  1. collections of field
  2. (*p).X <==> p.X,隐式间接引用,编译器会自动判断
  3. struct literals

array

  1. [n]T
  2. 数组长度(len)是数组类型的一部分,因此数组不能改变长度(否则类型就变了)

slice

  1. []T
  2. [low:high]T为半开区间,不包含high
  3. slice不存储数据,只是描述底层数组
  4. 修改slice的值,就会改变底层数组的值
  5. len and cap
    1. len,number of elements slice contains
    2. cap, number of elements in the underlying array,count from the first elements in the slice
  6. zero value is nil
  7. append,如果slice太小,会返回一个新的slice

map

  1. zero value is nil
  2. nil map 不能add
  3. 使用make初始化map
  4. 操作
    1. map[key] = v,insert/update
    2. delete(map, key)
    3. v, ok := map[key]

range

  1. for i := range map/slice,i为索引
  2. for i, v := range map/slice,v是值拷贝

function value

  1. go中函数可以是值
  2. 函数可以作为参数或者返回值
  3. 函数闭包(closures):闭包是一个函数值和引用的函数外部变量,函数绑定了被引用的变量,可以修改变量状态

Python

constant(常量)

  1. character,numeric,bool,string
  2. const
  3. no ":="
  4. numeric type

Flow control(控制结构)

for loop, while, if else, switch...

Golang

for loop

  1. for init;condition;post {}

    for i:=0; i<10; i++ {fmt.Println(i)}
  2. for i<100 {fmt.Println(i)}
  3. for {fmt.Println(i)}

if condition

  1. if
  2. if err := func();err != nil{}
  3. if...else

switch

  1. Go的swatch只执行选定的case,每个case后面默认break
  2. case无需为常量或整数
  3. from top to bottom

defer

  1. defer函数的参数会立即求值
  2. 多个defer按照LIFO原则执行

Python

并发

Golang

Python

网络

Golang

Python

OS接口

Golang

Python

Reference

comments powered by Disqus