douzhe9927 2018-02-11 21:41
浏览 31

如何在全局范围内将输出从select传递到select之外?

I cannot pass string variable outside of a select statement.

Case (simplified):

I have a synchronous script which uses select with two options

  1. timeout (3 seconds)
  2. default which is using channel to change value of a string.

When we check result of a string inside the select - it shows correctly.

But once we leave the select statement, variable is reverted to the value it was before.

package main

import (
    "log"
    "time"
)

func main() {
    timeout := time.After(3 * time.Second)
    pollInt := time.Second

    var ret string = "EMPTY"

OuterLoop:
    for {
        select {
        case <-timeout:
            log.Printf("[select][timeout] select timeout, break select and for loops")
            break OuterLoop

        default:
            log.Printf("[select][default] Processing our actions")

            ch := make(chan string, 1)

            go func(ch chan string) {
                var res string = "NOT EMPTY"
                ch <- res
            }(ch)

            select {
            case ret := <-ch:
                log.Printf("channel has a result")
                log.Printf("showing content of string AT THE SELECT: %v", ret)
            case <-time.After(1 * time.Second):
                log.Printf("timeout")

            }

        }
        log.Printf("[for] Sleep for a second..")
        time.Sleep(pollInt)
    }

    log.Printf("Starting to do some important actions.. (regardless was previous actions success or timeout)")
    /* ... */

    log.Printf("showing content of string: %v", ret)

    if ret == "NOT EMPTY" {
        log.Printf("SCRIPT FINISHED CORRECTLY")
    } else {
        log.Printf("SCRIPT FAILED")
    }

}

Result of script run:

2009/11/10 23:00:00 [select][default] Processing our actions
2009/11/10 23:00:00 channel has a result
2009/11/10 23:00:00 showing content of string AT THE SELECT: NOT EMPTY
2009/11/10 23:00:00 [for] Sleep for a second..
2009/11/10 23:00:01 [select][default] Processing our actions
2009/11/10 23:00:01 channel has a result
2009/11/10 23:00:01 showing content of string AT THE SELECT: NOT EMPTY
2009/11/10 23:00:01 [for] Sleep for a second..
2009/11/10 23:00:02 [select][default] Processing our actions
2009/11/10 23:00:02 channel has a result
2009/11/10 23:00:02 showing content of string AT THE SELECT: NOT EMPTY
2009/11/10 23:00:02 [for] Sleep for a second..
2009/11/10 23:00:03 [select][timeout] select timeout, break select and for loops
2009/11/10 23:00:03 Starting to do some important actions.. (regardless was previous actions success or timeout)
2009/11/10 23:00:03 showing content of string: EMPTY
2009/11/10 23:00:03 SCRIPT FAILED

But I'd expect:

2009/11/10 23:00:00 [select][default] Processing our actions
2009/11/10 23:00:00 channel has a result
2009/11/10 23:00:00 showing content of string AT THE SELECT: NOT EMPTY
2009/11/10 23:00:00 [for] Sleep for a second..
2009/11/10 23:00:01 [select][default] Processing our actions
2009/11/10 23:00:01 channel has a result
2009/11/10 23:00:01 showing content of string AT THE SELECT: NOT EMPTY
2009/11/10 23:00:01 [for] Sleep for a second..
2009/11/10 23:00:02 [select][default] Processing our actions
2009/11/10 23:00:02 channel has a result
2009/11/10 23:00:02 showing content of string AT THE SELECT: NOT EMPTY
2009/11/10 23:00:02 [for] Sleep for a second..
2009/11/10 23:00:03 [select][timeout] select timeout, break select and for loops
2009/11/10 23:00:03 Starting to do some important actions.. (regardless was previous actions success or timeout)
2009/11/10 23:00:03 showing content of string: NOT EMPTY
2009/11/10 23:00:03 SCRIPT FINISHED CORRECTLY

Playground: https://play.golang.org/p/uPKZtQQkm7p

Any ideas?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计