1_Packages_Variables_And_Functions

  • 每个go程序都是由包构成

  • (1) 同一个目录xx下的所有go文件,开头的package xxx中的xxx必须相同

    (然而神奇的是目录名称xx和package名xxx可以不相同)

    (2) 程序总是从__main package__开始运行,package名不是main的go文件即便定义了func main()也没用

    (3) import的是目录,调用的时候用的是包名

    (4) 同一个目录下,如果package名是main的话,那么只能有一个func main;如果有多个func main,需要在前面加上,并且再空一行再开始其他内容,否则会报错

      // +build ignore
    

    (5) 不同目录下可以建立多个名为package main的func main,分别进行测试(因为go允许目录名和包名不一致,所以可以暂时把包名改成main,测完了再改回来

  • 导入import

    (1) 可以使用多个import

      import "fmt"
      import "math"
    

    (2) 也可以使用分组导入,用括号把它们括起来(推荐

      import (
          "fmt"
          "math"
      )
    

    其中,fmt和math都是C:\Go\src下的包

  • 已导出exported

    (1) 如果一个名字以__大写字母__开头,就是__已导出的__

    (2) 在导入一个包时,只能引用其中__已导出__的名字;

    任何未导出的名字在该包外均无法访问,但是包内是自由访问的

      package main
    
      import (
          "fmt"
          "math"
      )
    
      func main() {
          fmt.Println(math.Pi)   //正确的写法
          fmt.Println(math.pi)   //会报错
      }
    
  • 注释

    (1) 单行注释: //

    (2) 多行注释: /* */

  • 函数

    (1) func开头

    (2) 可以有0个, 1个或多个参数

    (3) 类型在变量名之后

      package main
    
      import "fmt"
    
      func add(x int, y int) int {
          return x + y
      }
    
      func main() {
          fmt.Println(add(42, 13))
      }
    

    (4) 当连续两个或多个函数的已命名形参类型相同时,除最后一个类型以外,其它都可以省略

      package main
    
      import "fmt"
    
      func add(x, y int) int {
          return x + y
      }
    
      func main() {
          fmt.Println(add(42, 13))
      }
    

    (5) 函数可以返回__任意数量的返回值__

    函数返回值类型写在参数后面,大括号前面

      package main
    
      import "fmt"
    
      func swap(x, y string) (string, string) {
          return y, x
      }
    
      func main() {
          a, b := swap("hello", "world")
          fmt.Println(a, b)
      }
    

    (6) 返回值可以被命名,作为__文档__使用

      package main
    
      import "fmt"
    
      func split(sum int) (x, y int) {
    
          x = sum * 4 / 9
          y = sum - x
    
          return x, y
      }
    
      func main() {
    
          fmt.Println(split(17))
      }
    

    (7) 比较特殊的是error接口类型也作为返回值,类似于抛出异常

  • 变量

    (1) var用来声明一个变量(列表)

    (2) 和函数参数列表、返回值列表一样,声明的变量类型放在最后

    (3) var可以出现在包级别或函数级别

      package main
    
      import "fmt"
    
      var c, python, java bool
    
      func main() {
    
          var i int
          fmt.Println(i, c, python, java)
      }
    

    (4) 变量可以赋初值,如果赋初值的话可以不用指定变量类型

      package main
    
      import "fmt"
    
      var i, j int = 1, 2
      var k = "haha"
    
      func main() {
    
          var c, python, java = true, false, "no!"
    
          fmt.Println(i, j, c, python, java)
          fmt.Println(k)
      }
    

    (5) 在函数中,可以__用:=代替var__进行变量简短声明,但是要在类型明确的地方

    函数外声明变量只能用var

      package main
    
      import "fmt"
    
      func main() {
    
          var i, j int = 1, 2
          k := 3
          c, python, java := true, false, "no!"
    
          fmt.Println(i, j, k, c, python, java)
      }
    
  • 基本数据类型

    (1) bool

    (2) string

    (3) int int8 int16 int32 int64

    (4) uint uint8 uint16 uint32 uint64 uintptr

    (5) byte // uint8 的别名

    (6) rune // int32 的别名 // 表示一个 Unicode 码点

    (7) float32 float64

    (8) complex64 complex128

      package main
    
      import (
          "fmt"
          "math/cmplx"
      )
    
      var (       //声明变量也可以使用分组形式
    
          ToBe   bool       = false
          MaxInt uint64     = 1<<64 - 1
          z      complex128 = cmplx.Sqrt(-5 + 12i)
      )
    
      func main() {
    
          fmt.Printf("Type: %T Value: %v\n", ToBe, ToBe)
          fmt.Printf("Type: %T Value: %v\n", MaxInt, MaxInt)
          fmt.Printf("Type: %T Value: %v\n", z, z)
      }
    
  • 基本数据的__零值__

    (1) 没有明确初始值的变量声明会被赋予它们的零值

    (2) 数值类型为 0,布尔类型为 false,字符串为 “”(空字符串),数组、接口之类的是nil

  • (1) Go 在不同类型的项之间赋值时需要__显式转换__,无论是怎么转

    (2) 可以靠var声明的方式转,也可以靠:=的方式转

      package main
    
      import (
          "fmt"
          "math"
      )
    
      func main() {
    
          var x, y int = 3, 4
          var f float64 = math.Sqrt(float64(x*x + y*y))
          var z uint = uint(f)
    
          i := 42
          g := float64(i)
          u := uint(f)
    
          fmt.Println(x, y, z, i, g, u)
      }
    

    (3) 不显式指定变量类型时,可以自动进行__类型推导__

      package main
    
      import "fmt"
    
      func main() {
    
          v := 42.2 + 0.8i // 修改这里!
          fmt.Printf("v is of type %T\n", v)
      }
    
  • 常量

    (1) 使用 const 关键字声明

    (2) 常量可以是字符、字符串、布尔值或数值

    (3) 常量__不能__用 := 语法声明

      package main
    
      import "fmt"
    
      const Pi = 3.14
    
      func main() {
    
          const World = "世界"
    
          fmt.Println("Hello", World)
          fmt.Println("Happy", Pi, "Day")
    
          const Truth = true
          fmt.Println("Go rules?", Truth)
      }
    

    (4) 一个未指定类型的常量由上下文来决定类型

      package main
    
      import "fmt"
    
      const (
          // 将 1 左移 100 位来创建一个非常大的数字
          // 即这个数的二进制是 1 后面跟着 100 个 0
          Big = 1 << 100
          // 再往右移 99 位,即 Small = 1 << 1,或者说 Small = 2
          Small = Big >> 99
      )
    
      func needInt(x int) int { 
          return x*10 + 1 
      }
    
      func needFloat(x float64) float64 {
          return x * 0.1
      }
    
      func main() {
    
          fmt.Println(needInt(Small))
          //fmt.Println(needInt(Big))     //会报错
          fmt.Println(needFloat(Small))
          fmt.Println(needFloat(Big))
      }
    
Sure, I can help you with that! To set up a Python project named "memChar" with the mentioned functions, you can follow these steps: 1. Create a new directory and name it "memChar". 2. Open a terminal or command prompt, navigate to the "memChar" directory, and run the following command to create a new Python virtual environment: ``` python -m venv env ``` This will create a new directory named "env" inside the "memChar" directory, which will contain a clean Python environment with no packages installed. 3. Activate the virtual environment by running the following command: On Windows: ``` env\Scripts\activate.bat ``` On Linux/MacOS: ``` source env/bin/activate ``` This will activate the virtual environment and allow you to install packages without affecting the global Python environment. 4. Install the required packages by running the following command: ``` pip install pandas ``` This will install the "pandas" package, which we'll use to read and manipulate CSV files. 5. Create a new Python module named "memChar.py" inside the "memChar" directory, and define the required functions inside it. Here's an example implementation of the functions you mentioned: ```python import os import csv def txt_replace(filename, old_text, new_text): """Replace all occurrences of old_text with new_text in the given text file.""" with open(filename, 'r') as f: text = f.read() text = text.replace(old_text, new_text) with open(filename, 'w') as f: f.write(text) def find_and_insert_line(filename, search_text, new_line): """Find the first line containing search_text and insert new_line after it.""" with open(filename, 'r') as f: lines = f.readlines() for i, line in enumerate(lines): if search_text in line: lines.insert(i+1, new_line) break with open(filename, 'w') as f: f.writelines(lines) def find_and_replace_in_line(filename, search_text, old_text, new_text): """Find the first line containing search_text and replace all occurrences of old_text with new_text.""" with open(filename, 'r') as f: lines = f.readlines() for i, line in enumerate(lines): if search_text in line: lines[i] = line.replace(old_text, new_text) break with open(filename, 'w') as f: f.writelines(lines) def modify_files(path, filename_rule, modify_func): """Modify all files in the given path whose filenames match the given rule using the given modification function.""" for filename in os.listdir(path): if filename_rule in filename: filepath = os.path.join(path, filename) modify_func(filepath) def process_csv_file(filename, process_func): """Read the given CSV file and call the given process function for each row.""" with open(filename, 'r') as f: reader = csv.reader(f) for row in reader: process_func(*row) def generate_combinations(csv1_filename, csv2_filename, output_filename): """Generate all combinations of node names from the first CSV file and variables from the second CSV file, and write them to the output file.""" with open(csv1_filename, 'r') as f1, open(csv2_filename, 'r') as f2, open(output_filename, 'w') as fout: reader1 = csv.reader(f1) reader2 = csv.reader(f2) for node in reader1: for var in reader2: combination = node + var fout.write(','.join(combination) + '\n') ``` These functions should cover all the requirements you mentioned in your question. You can call these functions from another Python script or from the Python REPL to perform the desired operations. I hope this helps! Let me know if you have any further questions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值