英文:
How to use multiple expressions in single case statement?
问题
在下面的代码中:
package main
import (
"fmt"
"reflect"
)
type Model1 struct {
ID string
}
type Model2 struct {
ID string
}
func main() {
ch1 := make(chan Model1)
close(ch1)
checkIfChannelClosed(ch1)
ch2 := make(chan Model2)
close(ch2)
checkIfChannelClosed(ch2)
}
func checkIfChannelClosed(ch interface{}) bool {
if reflect.TypeOf(ch).Kind() != reflect.Chan {
fmt.Println("只有通道可以关闭")
return false
}
ok := true
if ch == nil {
return false
}
switch v := ch.(type) {
case chan Model1:
select {
case _, ok = <-v: // Line 26
default:
}
case chan Model2:
select {
case _, ok = <-v:
default:
}
default:
fmt.Println("无效的情况")
}
if ok {
fmt.Println("通道是打开的")
} else {
fmt.Println("通道已关闭")
}
return ok
}
GoLang编译器不允许在case语句中写入多个表达式(如下所示)。目标是避免为select
编写冗余代码:
switch v := ch.(type) {
case chan Model1, chan Model2:
select {
case _, ok = <-v:
default:
}
default:
fmt.Println("无效的情况")
}
如何在case语句中使用多个表达式?
英文:
In the below code:
package main
import (
"fmt"
"reflect"
)
type Model1 struct {
ID string
}
type Model2 struct {
ID string
}
func main() {
ch1 := make(chan Model1)
close(ch1)
checkIfChannelClosed(ch1)
ch2 := make(chan Model2)
close(ch2)
checkIfChannelClosed(ch2)
}
func checkIfChannelClosed(ch interface{}) bool {
if reflect.TypeOf(ch).Kind() != reflect.Chan {
fmt.Println("only channels can be closed")
return false
}
ok := true
if ch == nil {
return false
}
switch v := ch.(type) {
case chan Model1:
select {
case _, ok = <-v: // Line 26
default:
}
case chan Model2:
select {
case _, ok = <-v:
default:
}
default:
fmt.Println("Invalid case")
}
if ok {
fmt.Println("channel is open")
} else {
fmt.Println("channel is closed")
}
return ok
}
GoLang compiler does not allow to write multiple expressions in case statement(as shown below). Goal is to avoid redundant code for select
:
switch v := ch.(type) {
case chan Model1, chan Model2:
select {
case _, ok = <-v:
default:
}
default:
fmt.Println("Invalid case")
}
How to use multiple expressions with case statement?
答案1
得分: 1
我在《The Go programming language》的第7.13章节中读到了这段内容:
在这种风格中,重点是满足接口的具体类型,而不是接口的方法(如果有的话),并且没有隐藏信息。
所以,我认为x.(Type)返回的是一个具体类型。如果你在switch语句中使用多个case并且使用x:=x.(Type),在下面的代码中会发生什么?
switch v := ch.(type) {
case chan Model1,int:
//做一些操作
}
只需使用reflect.value来完成这个操作:
func checkIfChannelClosed(ch interface{}) bool {
v := reflect.ValueOf(ch)
if v.Kind() != reflect.Chan {
fmt.Println("只有通道可以关闭")
return false
}
_, ok := v.TryRecv()
if ok{
fmt.Println("从通道接收到值..")
}else{
fmt.Println("通道已关闭或接收无法完成而不阻塞")
}
return ok
}
英文:
I read this in "The Go programming language" chapter 7.13:
In this style, the emphasis is on the concrete types that satisfy the interface, not on the interface’s methods (if indeed it has any),and there is no hiding of information.
So, i think x.(Type) return a concrete type,if you use a multicase in a swith x:=x.(Type), what happend in the follow code?
switch v := ch.(type) {
case chan Model1,int:
//do something
}
just use the reflect.value to do this:
func checkIfChannelClosed(ch interface{}) bool {
v := reflect.ValueOf(ch)
if v.Kind() != reflect.Chan {
fmt.Println("only channels can be closed")
return false
}
_, ok := v.TryRecv()
if ok{
fmt.Println("recv value from channel..")
}else{
fmt.Println("channel is closed or receive cannot finish without blocking")
}
return ok
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论