This is a creation in Article, where the information may have evolved or changed.
Frequently Asked Questions
- The function returns the return XX, is it an atomic operation? (No, if you've seen the rain marks teacher's memory management, you know)
- What is the difference between a common function and a closure function? Where is the application scenario for the latter? (The closure function uses an external variable, which is a reference pass, and note that when the closure function runs, it gets the value of the variable when it is used.) Scenario: The closure function is smaller, a bit like an inline function, for other scenarios such as defer-close connection, etc.)
- What is the difference between make and new (made for channel, map, and slice to create and initialize 0 values. New is a pointer object. Effective go in one example)
var p *[]int = new([]int) // p=nil, 没有任何用处,只是分配了一个指针对象var v []int = make([]int, 100) // 而make则分配了长度和容量等于100,并且初始化零值的对象// 复杂方法:var p *[]int = new([]int)*p = make([]int, 100, 100)// 常用方法:v := make([]int, 100)
Find duplicate rows
Code Snippets
func findDup() { scanner:=bufio.NewScanner(os.Stdin) for scanner.Scan(){ maps[scanner.Text()]++ } for line, count := range maps{ if count >0 { fmt.Printf("line: %s, count: %d\n", line, count) } }}
Bufio is a Golang standard library that represents the meaning of buffered IO and is readable and writable. Read, process, and output the input data via the BUFIO standard library
Here we can understand and use the methods commonly used in the BUFIO library:
The Bufio implements IO. Reader and Io.writer interface, so readable and writable.
Bufio. Reader function is to read the Oldreader data into the buffer, its common method list
reader:=bufio.NewReader(oldReader) // 默认size为4096reader:=bufio.NewReaderSize(oldReader, size)bytes, error = reader.ReadSlice(delim/*结束符*/)bytes, error = reader.ReadString(delim)bytes, error = reader.ReadLine() // 默认换行符\nbytes, error = reader.ReadBytes(delim)n, error = reader.Reader([]byte)
Bufio. Reader using Demo:
reader:=bufio.NewReader(strings.NewReader("ABCDEFG\nHIJKLMN\n")var bts = make([]byte, 100) // 注意点:需要初始化_, err = reader.Read(bts)
One thing to keep in mind here: var BTS = make ([]byte, 100), requires initialization. The slice type variable is initialized first, and if it is not initialized, if it is initialized in the called method body, the slice type variable or the old value is returned. Reason Description:
- The slice layer is actually a struct structure type with len,cap and pointer arrays. So the function body initialization is not consistent between the memory address and the incoming memory address, the data read is old
- If the incoming parameter is initialized, it is equivalent to Func (temp=struct{}/ incoming parameter param/), although the address is different, but is re-assigned, and Len, the CAP value is the same, the underlying pointer array is still pointing to the same memory address, If the memory address of the underlying array changes, because the pointer to the array does not change, the incoming parameter points to the new data in the underlying array.
Conclusion: Therefore, whenever a slice type variable is encountered, it needs to be initialized as a parameter, and the length len of the slice is specified.