执行方式 |
1 交互模式: $ pyhthon >>> print “Hello, World!” 2 文件模式 $ echo ‘print “Hello, World!”’ > test.py $ python test.py Hello, World! 3 命令模式: 修改文件 test.py: #!/usr/bin/python print “Hello, Python!” 修改变成可执行文件 $ chmod +x test.py $./test.py |
package main
import “fmt”
func main() { fmt.Println(“Hello, World!”: }
$ go run |
标识符 |
大写:类名 _A: 私有 __A:强私有 _A_:语言特殊定义 |
大写:公共标识符 其他:私有标识符 |
判断 |
if True: print “True” else: print “False” if (i == 100): print “i = 100” 其他: elif |
if true { fmt.Println(“True”) } else { fmt.Println(“False”) }其他: else if |
循环 |
fruits = [“apple”, “banana”, “cherry”] for x in fruits: print(x) 或者: for x in range(0, len(fruits), 1): print(x) 或者: i = 1 while i < 3: print(i) i += 1 其他: break, continue |
fruits := []string{“apple”, “banana”, “cherry”} for _, x := range fruits { fmt.Println(x) } 或者: for i:= 0; i < len(fruits); i++ { fmt.Println(fruits[i]); } 其他: break, continue |
注释 |
# This is a comment. print(“Hello, World!”) |
// This is a comment. fmt.Println(“Hello, World!”) /* This comment can cross multiple lines. */ |
字符串 |
‘hello’ “hello” ”“” Hello, World!“”” |
‘c’ // character “string” ` Hello, World!` |
list |
thislist = [“apple”, “banana”, “cherry”] print(thislist) print(thislist[1]) thislist.append(“orange”) thislist.insert(1, “orange”) thislist.remove(“banana”) thislist.pop() del thislist[0] thislist.clear() print(len(thislist)) del thislist |
thislist := string{“apple”, “banana”, “cherry”} fmt.Println(thislist) fmt.Println(thislist[1]) thislist = thislist.append(“orange”) // no simple operation for insert // and remove // and pop // and delete // and clear fmt.Println(len(thislist)) thislist = nil |
tuple |
A tuple is a collection which is ordered and unchangeable
thistuple = (“apple”, “banana”, “cherry”) print(thistuple) print(thistuple[1]) thistuple[1] = “blackcurrant” if “apple” in thistuple: print(“Yes, ‘apple’ is in the fruits tuple”) |
There is no tuple in go, but how about https://github.com/kmanley/golang-tuple |
set |
thisset = {“apple”, “banana”, “cherry”} print(thisset)
print(“banana” in thisset)
thisset.add(“orange”) thisset.remove(“banana”) |
m := map[string]bool for _, x := range m { fmt.Println(x) } x, ok := m[“banana”] fmt.Println(ok) m[“orange”]=true delete(m, “banana”) |
dict |
thisdict = { “brand”: “Ford”, “model”: “Mustang”, “year”: 1964 } print(thisdict) |
thisdict := map[string]string{ “brand”: “Ford”, “model”: “Mustang”, “year”: 1964, } fmt.Println(thisdict) |
fucntions |
def my_function(x): return 5 * x my_function(3) |
func my_function(x float64) float64 { return 5 * x } my_function(3) |
lambda |
x = lambda a : a + 10 print(x(5)) |
x := func(a int) int { return a + 10} fmt.Println(x(5)) |
class |
MyClass: x = 5 p1 = MyClass() print(p1.x |
type MyClass struct { x int } p1 := MyClass{5} fmt.Println(p1.x) |
_init_ |
class Person: def init(self, name, age): self.name = name self.age = age
def myfunc(self): print(“Hello my name is ” + self.name)
p1 = Person(“John”, 36)
print(p1.name) print(p1.age) |
type Person struct { name string age int }
func NewPerson(name string, age int) *Person { p := &Person{name, age} return p }
func (self *Person) myfunc() { fmt.Println(“Hello my name is ” + self.name) }p1 := NewPerson(“John”, 36)
fmt.Println(p1.name, p1.age) |
import |
import mymodule as mx |
import mx “mymodule” |
try…catch |
try: f = open(“demofile.txt”) f.write(“Lorum Ipsum”) except: print(“Something went wrong when writing to the file”) finally: f.close() |
func main() { defer func(){ if err:=recover();err!=nil{ fmt.Println(err) } }() file, err := os.Open(“demofile.txt”) if err != nil { panic(errors.New(“Someting went wrong when writing to the file”)) } file.WriteString(“Lorum Ipsum”) } |