2019年3月8日
CSP: “顺序通信进程”(communicating sequential processes)或被简称为CSP。 CSP是一种现代的并发编程模型,在这种编程模型中值会在不同的运行实例(goroutine)中传递。
一、Goroutines 定义:在GO中,每个并发的执行单元被称为一个goroutine go语句: 在函数或方法前加上关键字go,就形成了go语句。go语句使函数或方法在新建的goroutine中执行。 程序启动时,主函数(main函数)会在main goroutine中执行。当main goroutine退出时,所有其它goroutine都会退出。 f() // call f(); wait for it to return go f() // create a new goroutine that calls f(); don't wait func main() { l, err := net.Listen("tcp", "192.168.0.1:8000") if err != nil { log.Fatal(err) } for { conn, err := l.Accept() if err != nil { log.Print(err) continue } go handleConn(conn) } } 二、Channels channel 是 goroutines之间通信的机制 通过make来创建一个channel 与map类似,channel也是能底层数据结构的引用,作为函数参数传递时,是传递的引用。 channel的零值为nil。 channel之间可以用 == 进行比较。 一个channel有发送和接收两个操作,都是通信行为。 发送将一个值从一个goroutine发送到另一个接收该值的goroutine。 发送和接收都使用 <- 运算符号,发送时,<-分割channel和要发送的值。接收时,<-放在channel之前。 在关闭的channel上发送数据会引起panic, 但可以接收数据,如果被关闭的channel中没有数据,则得到零值。 ch <- x // a send statement y := <-ch // a receive expression in an assignment statement <-ch // a receive statement; result is discarded close(ch) // close channel 三、不带缓冲的channel ch = make(chan int) // unbuffered channel ch = make(chan int, 0) // unbuffered channel ch = make(chan int, 3) // buffered channel with capacity 3 在无缓存的channel上进行收发操作,将会导致此goroutine阻塞,直到有另一个goroutine对该通道进行发收操作。 无缓存的channel上的收发,导致两个goroutine进行一次同步操作。所以,无缓存channel也叫同步channel。 当我们说x事件既不是在y事件之前发生也不是在y事件之后发生,我们就说x事件和y事件是并发的。 func main() { conn, err := net.……
阅读全文
2019年3月8日
c++11之前 c++11之前,auto关键字用来表示变量的存储期
类型推断 变量声明时可以用auto,编译器会自动推断数据类型
auto i = 10; std::vector<int> v; for (auto it = v.begin(); it != v.end(); it++) { std::cout << *v << std::endl; } 可见,auto使代码更简洁,增强了可读性。……
阅读全文
2019年3月8日
what? 智能指针是基于RAII思想的c++类(类模板)
why? 智能指针帮助c++使用者更好的管理内存
how? 使用方法
eg:
std::shared_ptr<int> foo = std::make_shared<int> (10); // same as: //std::shared_ptr<int> foo (new int(10)); std::cout << "*foo: " << *foo << std::endl; eg:
class A { public: A(int i) : m_i(i) { } void foo() { std::cout << m_i << std::endl; } private: int m_i; }; typedef std::shared_ptr<A> A_PTR; A_PTR aPtr = std::make_shared<A>(10); std::cout << aPtr->foo() << std::endl; ……
阅读全文
2019年3月8日
语言支持 Go语言原生支持Unicode,它可以处理全世界任何语言的文本。
语言组织 Go语言代码由包组织而成 一个目录定义一个包,目录中存放N个.go源码包 包通过import关键字导入到其它的包中使用 Go采用静态编译,将所有第三方依赖打包编译为一个光秃秃的可执行文件 Go语言只有一种循环语言,就是for语句 printf常用转换字符 %d 十进制整数
%x, %o, %b 十六进制,八进制,二进制整数。
%f, %g, %e 浮点数: 3.141593 3.141592653589793 3.141593e+00
%t 布尔:true或false
%c 字符(rune) (Unicode码点)
%s 字符串
%q 带双引号的字符串"abc"或带单引号的字符'c'
%v 变量的自然形式(natural format)
%T 变量的类型
%% 字面上的百分号标志(无操作数)
……
阅读全文
2019年3月6日
一、概念 接口类型是一种抽象的类型。它只展示出类型的方法,不做具体的实现。
// package fmt func Fprintf(w io.Writer, format string, args ...interface{}) (int, error) func Printf(format string, args ...interface{}) (int, error) { return Fprintf(os.Stdout, format, args...) } func Sprintf(format string, args ...interface{}) string { var buf bytes.Buffer Fprintf(&buf, format, args...) return buf.String() } //io.Writer为接口类型 package io // Writer is the interface that wraps the basic Write method. type Writer interface { Write(p []byte) (n int, err error) } //io.Writer实现示例 type ByteCounter int func (c *ByteCounter) Write(p []byte) (int, error) { *c += ByteCounter(len(p)) // convert int to ByteCounter return len(p), nil } var c ByteCounter c.……
阅读全文