基础代码 **************************** .. include:: project.rst .. include:: helloworld.rst 注释 =========================== .. tabs:: .. tab:: 编程语言 .. tabs:: .. tab:: python .. literalinclude:: /../../basicCode/comment/main.py :language: python .. note:: 开源代码参考 * `sqlalchemy.engine.create::create_engine `_ .. code-tab:: go /* 多行注释 */ // 单行注释 .. code-tab:: javascript /* 多行注释 */ // 单行注释 .. code-tab:: java /* 多行注释 */ // 单行注释 .. tab:: DSL .. tabs:: .. tab:: jinja2 .. code-block:: jinja {# note: commented-out template because we no longer use this {% for user in users %} ... {% endfor %} #} 声明变量 ============================================== .. tabs:: .. tab:: Python .. literalinclude:: /../../basicCode/variable/variable.py :language: python .. tab:: Golang .. literalinclude:: /../../basicCode/variable/variable.go :language: go .. tab:: Javascript .. literalinclude:: /../../basicCode/variable/variable.js :language: javascript 常量 ============================================ .. tabs:: .. tab:: Python .. literalinclude:: /../../reference/basic/constant/constant.py :language: python .. tab:: Golang .. literalinclude:: /../../reference/basic/constant/constant.go :language: go .. tab:: Javascript .. literalinclude:: /../../reference/basic/constant/constant.js :language: javascript 字符串 ========================================= .. tabs:: .. tab:: Python .. literalinclude:: /../../reference/basic/string/_string.py :language: python .. tab:: Golang .. literalinclude:: /../../reference/basic/string/string.go :language: go .. tab:: Javascript .. literalinclude:: /../../reference/basic/string/string.js :language: javascript .. include:: list.rst .. include:: array.rst .. include:: dict.rst .. include:: typing.rst 条件判断 =============================================== .. tabs:: .. tab:: 编程语言 .. tabs:: .. code-tab:: py def if_else_case(a: int): if a == 0: print("执行 if 语句块") elif a == 1: print("执行 else if 语句块") else: print("执行 else 语句块") .. tab:: Golang .. literalinclude:: /../../reference/basic/condition/condition.go :language: go .. tab:: SQL .. tabs:: .. tab:: mysql .. literalinclude:: /../../reference/basic/condition/condition.sql :language: mysql 循环 ======================== .. tabs:: .. code-tab:: py for i in range(10): if i == 5: continue print("位置1 执行 for 语句块 i:", i) i = 0 b = True while b: print("位置2 执行 for 语句块 i:", i) i += 1 if i >= 10: b = False # 列表循环 LIST = [1, 2, 3, 4, 5, 6, 7, 8, 9] for index, data in enumerate(LIST): print("位置3 执行 for 语句块 index: {}, data: {}".format(index, data)) # 字典循环 mp = {"A": "a", "B": "b", "C": "c"} for key, value in mp.items(): print("位置4 执行 for 语句块 key: {}, value: {}".format(index, value)) # 字符串循环 STR = "今天天气很好" for index, char in enumerate(STR): print("位置5 执行 for 语句块 key: {}, char: {}".format(index, char)) # 死循环 j = 0 while True: print("位置6 执行死循环 j:", j) j += 1 if j >= 10: break .. code-tab:: go package main import "fmt" func forCase(){ for i := 0; i < 10; i++ { if i == 5 { continue } fmt.Println("位置1 执行 for 语句块 i:", i) } var i int var b = true for b { fmt.Println("位置2 执行 for 语句块 i:", i) i++ if i >= 10 { b = false } } // 切片循环 list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} for index, data := range list { fmt.Printf("位置3 执行 for 语句块 index: %d, data: %d \n", index, data) } // 字典循环 mp := map[string]string{"A": "a", "B": "b", "C": "c"} for key, value := range mp { fmt.Printf("位置4 执行 for 语句块 key: %v, value: %v \n", key, value) } // 字符串循环 str := "今天天气很好" for index, char := range str { fmt.Printf("位置5 执行 for 语句块 index: %v, char: %v \n", index, string(char)) } var j int for { fmt.Println("位置6 执行 for 语句块 j:", j) j++ if j >= 10 { break } } for i := 0; i < 2; i++ { for j := 0; j < 2; j++ { fmt.Printf("位置7 执行 for 语句块 i: %d, j: %d\n", i, j) } } lab1: for i := 0; i < 2; i++ { for j := 0; j < 2; j++ { fmt.Printf("位置8 执行 for 语句块 i: %d, j: %d\n", i, j) break lab1 } } } .. tab:: MySQL while .. literalinclude:: /../../reference/basic/for/mysql/while.sql :language: mysql loop .. literalinclude:: /../../reference/basic/for/mysql/loop.sql :language: mysql switch ============================ .. tabs:: .. code-tab:: py # match表面上像C、Java或JavaScript中的switch语句,但其实它更像Rust或Haskell中的模式匹配。 def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something's wrong with the internet" .. tab:: Go .. literalinclude:: /../../reference/basic/switch/main.go :language: go .. code-tab:: mysql create procedure proc_statement_case (in parameter int) begin declare var int; set var=parameter+1; case var when 0 then insert into userinfo values(4, 'zhangsan4', 18); when 1 then insert into userinfo values(5, 'zhangsan5', 18); else insert into userinfo values(6, 'zhangsan6', 18); end case; end; 异常 ====================== .. tabs:: .. tab:: Python .. literalinclude:: error/main.py :language: python **else子句** try … except 语句有一个可选的 else 子句,在使用时必须放在所有的 except 子句后面。对于在try 子句不引发异常 时必须执行的代码来说很有用。例如: .. code-block:: python for arg in sys.argv[1:]: try: f = open(arg, 'r') except OSError: print('cannot open', arg) else: print(arg, 'has', len(f.readlines()), 'lines') f.close() **finally** .. code-block:: pycon >>> try: ... raise KeyboardInterrupt ... finally: ... print('Goodbye, world!') ... Goodbye, world! KeyboardInterrupt Traceback (most recent call last): File "", line 2, in 如果存在 finally 子句,则 finally 子句将作为 try 语句结束前的最后一项任务被执行。 finally 子句不论 try 语句是否产生了异常都会被执行。 **异常链** raise 语句允许可选的 from 子句,它启用了链式异常。 例如:: # exc must be exception instance or None. raise RuntimeError from exc .. tip:: 用途: 转换异常 禁用异常链可使用 from None 习语 **打印异常内容** .. code-block:: python import traceback try: print(1/0) except Exception: # 输出到控制台 traceback.print_exc() # 保存到变量 err = traceback.format_exc() .. tab:: Golang .. literalinclude:: error/main.go :language: go .. tab:: Mysql .. literalinclude:: error/main.sql :language: mysql .. include:: import.rst .. include:: function.rst .. include:: trycatch.rst .. include:: debugger.rst .. include:: interface.rst .. include:: T.rst