在golang的Switch语句中,无法识别来自通道的字符串变量。

huangapple go评论81阅读模式
英文:

Does not recognize string variable coming from a channel in a Switch statement golang

问题

这个函数通过将m作为参数传递给goroutine来调用。

m中传递的值是字符串:"01a",而Switch语句无法识别。

func myfunc(m string, c chan string) {
    defer close(c)

    switch m {
    case "01a":
       msg_out = "NO PASS"
    }
    c <- msg_out
}

当将m设置为"01a"时,Switch正常工作。

func myfunc(m string, c chan string) {
    defer close(c)
    
    m = "01a"
    switch m {
    case "01a":
        msg_out = "PASS"
    }
    c <- msg_out
}

我怀疑通道可能会引入其他隐藏字符。

英文:

This function is called from a goroutine by passing as parameter m.

The value sent in m is the string: "01a" and the statement Switch does not recognize

func myfunc(m string, c chan string) {
    defer close(c)

    switch m {
    case &quot;01a&quot;:
       msg_out = &quot;NO PASS&quot;
    }
    c &lt;- msg_out
}

when set m the Switch works fine

func myfunc(m string, c chan string) {
    defer close(c)
    
    m = &quot;01a&quot;
    switch m {
    case &quot;01a&quot;:
        msg_out = &quot;PASS&quot;
    }
    c &lt;- msg_out
}

I suspect the channel will introduce other hidden characters

答案1

得分: 1

你的代码的意图不清楚,提供的代码是无效的。在提供的两个示例中,你都没有显示尝试从通道中读取数据,两个示例都是根据字符串进行切换,然后将一个新值赋给未声明的msg_out变量,并将该值发送到通道中。

没有完整的代码,无法确定你出错的地方。下面是一个简单的示例,演示了如何将文本发送到通道并再次读取出来。希望这能澄清过程,并确认字符串可以正常通过通道发送。

如果你仍然无法使其工作,我认为你需要发布完整的代码示例以获得帮助。

package main

import (
	"log"
	"time"
)

func main() {

	// 创建通道
	strChan := make(chan string)

	// 启动一个goroutine来监听通道
	go func(strChan chan string) {

		// 循环等待通道上的输入
		for {
			select {
			case myString := <-strChan:
				// 在这里运行接收到字符串时的代码

				// 根据字符串的实际内容进行切换
				switch myString {
				case "I'm the expected string":
					log.Println("收到了预期的字符串")
				default:
					log.Println("不是我要找的字符串")
				}
			}
		}
	}(strChan)

	// 等待一段时间,然后在通道上发送一个字符串
	time.Sleep(time.Second * 2)
	strChan <- "I'm the expected string"

	// 再次等待,给通道响应的机会
	time.Sleep(time.Second * 2)
}

Go playground

英文:

It's not clear what your code is trying to do, the code provided is invalid.
You haven't shown any attempt to read from the channel in either example provided, both examples switch on a string then assign a new value to msg_out (which is not declared) and send that value down a channel.

Without the full code it's impossible to tell where you are going wrong, here is a simple example of sending text down a channel and reading it off again. Hopefully this will clarify the process and confirm that strings are sent over channels just fine.

If you still cant get it working, I think you will need to post full code example to get help.

Go playground

    package main
    
    import (
    	&quot;log&quot;
    	&quot;time&quot;
    )
    
    func main() {
    
    	//make the channel
    	strChan := make(chan string)
    
        //launch a goroutine to listen on the channel
    	go func(strChan chan string) {

            //loop waiting for input on channel
    		for {
    			select {
    			case myString := &lt;-strChan:
    				// code to be run when a string is recieved here
    
    				// switch on actual contents of the string
    				switch myString {
    				case &quot;I&#39;m the expected string&quot;:
    					log.Println(&quot;got expected string&quot;)
    				default:
    					log.Println(&quot;not the string I&#39;m looking for&quot;)
    				}
    			}
    		}
    	}(strChan)
    
    	//sleep for a bit then send a string on the channel
    	time.Sleep(time.Second * 2)
    	strChan &lt;- &quot;I&#39;m the expected string&quot;

    	//wait again, gives channel response a chance to happen  
        time.Sleep(time.Second * 2)
}

huangapple
  • 本文由 发表于 2017年8月20日 02:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/45775183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定