go

【基本流程控制】4.select语句

橙三吉。 提交于 2020-12-31 09:16:30
Go语言-select语句 select 语句属于条件分支流程控制方法,不过它只能用于通道。它可以包含若干条 case 语句,并根据条件选择其中的一个执行。进一步说, select 语句中的 case 关键字只能后跟用于通道的发送操作的表达式以及接收操作的表达式或语句。示例如下: ch1 := make(chan int, 1) ch2 := make(chan int, 1) // 省略若干条语句 select { case e1 := <-ch1: fmt.Printf("1th case is selected. e1=%v.\n", e1) case e2 := <-ch2: fmt.Printf("2th case is selected. e2=%v.\n", e2) default: fmt.Println("No data!") } 如果该 select 语句被执行时通道 ch1 和 ch2 中都没有任何数据,那么肯定只有 default case 会被执行。但是,只要有一个通道在当时有数据就不会轮到 default case 执行了。显然,对于包含通道接收操作的 case 来讲,其执行条件就是通道中存在数据(或者说通道未空)。如果在当时有数据的通道多于一个,那么Go语言会通过一种伪随机的算法来决定哪一个 case 将被执行。 另一方面,对于包含通道发送操作的

Selenium3自动化测试【11】Katalon Studio案例

坚强是说给别人听的谎言 提交于 2020-12-31 08:43:06
基于前面Katalon Studio案例,通过Katalon Recorder轻松实现数据驱动测试。 测试场景,启动录制后,以bing搜索页面中进行如下操作:  输入Bella;  单击搜素按钮 ;  关闭浏览器;  Katalon Recorder工具栏中单击Stop。 (1) 录制完毕停止后,形成基础场景,,如图所示。 (2) 创建一个JSON文件,文件命名为【SearchWord.json】,内容如下: [ { "SearchValue": "Bella" }, { "SearchValue": "leo" } ] (3) 在Data Driven区域,单击【Add JSON File】,加载刚刚创建的【SearchWord.json】,如所示。 (4) 右键单击“id=sb_form_q”所在行,选择“Add Command”。成功在“id=sb_form_q”下一行添加一个空白行,如图所示。 (5) 选中添加的空白行,在编辑区域填写内容,Command区域下拉选择“loadVars”,Target区域填写的值为“SearchWord.json”,与创建的JSON文件同名,如图所示。 (6) 右键单击“id=sb_form_go”所在行,选择“Add Command”。成功在“id=sb_form_go”下一行添加一个空白行。选中添加的空白行,在编辑区域填写内容

Appending to go lang slice using reflection

最后都变了- 提交于 2020-12-30 10:45:08
问题 For some reason, it appears that adding new element to slice using reflection doesn't update slice itself. This is the code to demonstrate: package main import ( "fmt" "reflect" ) func appendToSlice(arrPtr interface{}) { valuePtr := reflect.ValueOf(arrPtr) value := valuePtr.Elem() value = reflect.Append(value, reflect.ValueOf(55)) fmt.Println(value.Len()) // prints 1 } func main() { arr := []int{} appendToSlice(&arr) fmt.Println(len(arr)) // prints 0 } Playground link : https://play.golang

Appending to go lang slice using reflection

巧了我就是萌 提交于 2020-12-30 10:41:21
问题 For some reason, it appears that adding new element to slice using reflection doesn't update slice itself. This is the code to demonstrate: package main import ( "fmt" "reflect" ) func appendToSlice(arrPtr interface{}) { valuePtr := reflect.ValueOf(arrPtr) value := valuePtr.Elem() value = reflect.Append(value, reflect.ValueOf(55)) fmt.Println(value.Len()) // prints 1 } func main() { arr := []int{} appendToSlice(&arr) fmt.Println(len(arr)) // prints 0 } Playground link : https://play.golang

Correct way to write a text SQL query in Go

一世执手 提交于 2020-12-30 10:39:47
问题 I can't find a good example of the right way to concat the string portion of a text query with the values. For example: query := `SELECT column_name FROM table_name WHERE column1_name = %d AND column2_name = %d` % (val1, val2) rows, res, err := db.Query(query) This doesn't work. The compiler returns syntax error: unexpected comma, expecting ) Likely because I'm trying to use a python style tuple. If I rewrite it as query := `SELECT column_name FROM table_name WHERE column1_name = %d AND

Correct way to write a text SQL query in Go

混江龙づ霸主 提交于 2020-12-30 10:38:06
问题 I can't find a good example of the right way to concat the string portion of a text query with the values. For example: query := `SELECT column_name FROM table_name WHERE column1_name = %d AND column2_name = %d` % (val1, val2) rows, res, err := db.Query(query) This doesn't work. The compiler returns syntax error: unexpected comma, expecting ) Likely because I'm trying to use a python style tuple. If I rewrite it as query := `SELECT column_name FROM table_name WHERE column1_name = %d AND

Correct way to write a text SQL query in Go

烈酒焚心 提交于 2020-12-30 10:37:28
问题 I can't find a good example of the right way to concat the string portion of a text query with the values. For example: query := `SELECT column_name FROM table_name WHERE column1_name = %d AND column2_name = %d` % (val1, val2) rows, res, err := db.Query(query) This doesn't work. The compiler returns syntax error: unexpected comma, expecting ) Likely because I'm trying to use a python style tuple. If I rewrite it as query := `SELECT column_name FROM table_name WHERE column1_name = %d AND

go-zero 编写项目(微服务架构)的思路

喜夏-厌秋 提交于 2020-12-30 10:01:21
微服务:由多个服务,和常见的一个网关入口组成。 图: 比如服务1,rpc 里有 rpc 接口 10个。可能通过 http api 网关对外服务的只有 4 个,余6个是提供给其它的 rpc 服务使用的。 只是一种情况。 这里 go-zero 开发框架就使用这种样子。 那么参考 go-zero 的文档: https://www.yuque.com/tal-tech/go-zero/yaoehb 生成网关: goctl api -o bookstore.api 执行地址默认是:localhost:8888 生成服务: goctl rpc template -o add.proto 这里先编写服务,再把要公开服务的接口写到网关中,再生成网关。 服务1的默认地址是:localhost:8080 服务2的默认地址是:localhost:8081 服务1,服务2,这样一遍一遍又一遍。 子子孙孙无穷匮也,而需求不停增,何苦而停也? 来源: oschina 链接: https://my.oschina.net/u/4370441/blog/4869138

Override an external package's cgo compiler and linker flags?

感情迁移 提交于 2020-12-30 06:34:08
问题 Let's say I want to use some awesome go package. I can include it by: import "github.com/really-awesome/project/foobar" And inside that project's foobar.go file, it defines some cgo instructions like: #cgo windows CFLAGS: -I C:/some-path/Include #cgo windows LDFLAGS: -L C:/some-path/Lib -lfoobar But if I have that foobar C dependency installed somewhere else, I would really need those lines to say: #cgo windows CFLAGS: -I C:/different-path/Include #cgo windows LDFLAGS: -L C:/different-path

How to change external variable's value inside a goroutine closure

你说的曾经没有我的故事 提交于 2020-12-30 04:10:00
问题 func (this *l) PostUpload(ctx *Context) { //ctx.Response.Status = 500 l, err := models.NewL(this.Config) go func() { err = l.Save(file) if err != nil { ctx.Response.Status = 500 ctx.Response.Body = err } else { ctx.Response.Status = 204 } }() } How to change ctx.Response.Status value inside the goroutine closure? 回答1: You have no guarantee to observe changes made to the value of a variable in another goroutine without synchronization . See The Go Memory Model for details. So if you want to