英文:
Convert Redis subscribe []uint8 output to string
问题
我正在使用下面提到的代码从Redis发布-订阅中获取输出。在Redis发布期间传递的消息是一个字符串(test-message
)。
然而,在订阅阶段获得的输出类型是[]uint8
。当我运行下面提到的代码时,我得到的输出是[116 101 115 116 45 109 101 115 115 97 103 101]
(而不是预期的test-message
)。
这是由下面提到的代码中的这一行引起的:fmt.Println("Output: ", v.Data, reflect.TypeOf(v.Data)).
我如何在上述行中获得我所期望的正确输出(即test-message
)? 我觉得我可能需要将[]uint8
转换为string
以获得正确的输出。
我的代码如下所示。我使用这个好的答案作为我的代码参考。
package main
import (
"fmt"
"log"
"reflect"
"time"
"github.com/gomodule/redigo/redis"
)
func main() {
fmt.Println("Start redis test.")
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
log.Println(err)
} else {
log.Println("No error during redis.Dial.")
}
// defer c.Close()
val := "test-message"
/// Publisher.
go func() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
panic(err)
}
count := 0
for {
c.Do("PUBLISH", "example", val)
// c.Do("PUBLISH", "example",
// fmt.Sprintf("test message %d", count))
count++
time.Sleep(1 * time.Second)
}
}()
/// End here
/// Subscriber.
psc := redis.PubSubConn{Conn: c}
psc.Subscribe("example")
for {
switch v := psc.Receive().(type) {
case redis.Message:
//fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
fmt.Println("Output: ", v.Data, reflect.TypeOf(v.Data))
case redis.Subscription:
fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
fmt.Println(v)
}
time.Sleep(1)
}
/// End here
}
英文:
I am using the code mentioned below to get output from Redis Publish-Subscribe. The message passed during the Redis publish a string (test-message
).
However, the output that I get during subscribe stage is of type []uint8
. Following is the output that I get when I run the below mentioned code [116 101 115 116 45 109 101 115 115 97 103 101]
(instead of test-message
which is the intended output.
This is caused by this line in the below mentioned code fmt.Println("Output: ", v.Data, reflect.TypeOf(v.Data)).
How can I get correct output that I desire in Subscribe in the aforesaid line (i.e. test-message
)? I feel that I may need to convert from []uint8
to string
to get the correct output.
My code is given below. I used this good answer as a reference for my code.
package main
import (
"fmt"
"log"
"reflect"
"time"
"github.com/gomodule/redigo/redis"
)
func main() {
fmt.Println("Start redis test.")
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
log.Println(err)
} else {
log.Println("No error during redis.Dial.")
}
// defer c.Close()
val := "test-message"
/// Publisher.
go func() {
c, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
panic(err)
}
count := 0
for {
c.Do("PUBLISH", "example", val)
// c.Do("PUBLISH", "example",
// fmt.Sprintf("test message %d", count))
count++
time.Sleep(1 * time.Second)
}
}()
/// End here
/// Subscriber.
psc := redis.PubSubConn{Conn: c}
psc.Subscribe("example")
for {
switch v := psc.Receive().(type) {
case redis.Message:
//fmt.Printf("%s: message: %s\n", v.Channel, v.Data)
fmt.Println("Output: ", v.Data, reflect.TypeOf(v.Data))
case redis.Subscription:
fmt.Printf("%s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
fmt.Println(v)
}
time.Sleep(1)
}
/// End here
}
答案1
得分: 3
[]uint8
是与 []byte
同义词,可以使用转换表达式将字节切片转换为 string
。
> 2. 将字节切片转换为字符串类型会生成一个字符串,其中连续的字节是切片的元素。
>
> none > string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø" > string([]byte{}) // "" > string([]byte(nil)) // "" > > type MyBytes []byte string(MyBytes{'h', 'e', 'l', 'l', '\xc3', > '\xb8'}) // "hellø" >
所以以下代码就足够了:
string(v.Data)
你也可以使用 fmt
中的 %s
格式化动词来打印字符串而无需转换:
fmt.Printf("Output: %s", v.Data)
示例:https://play.golang.org/p/JICYPfOt-fQ
data := []uint8(`test-message`)
fmt.Println(data)
fmt.Println(string(data))
fmt.Printf("%s\n", data)
英文:
[]uint8
is synonymous with []byte
and a byte slice can be converted to string
using a conversion expression.
> 2. Converting a slice of bytes to a string type yields a string whose successive bytes are the elements of the slice.
>
> none
> string([]byte{'h', 'e', 'l', 'l', '\xc3', '\xb8'}) // "hellø"
> string([]byte{}) // ""
> string([]byte(nil)) // ""
>
> type MyBytes []byte string(MyBytes{'h', 'e', 'l', 'l', '\xc3',
> '\xb8'}) // "hellø"
>
So the following should be enough:
string(v.Data)
You can also print the string without conversion by using the %s
verb in fmt
:
fmt.Printf("Output: %s", v.Data)
Example: https://play.golang.org/p/JICYPfOt-fQ
data := []uint8(`test-message`)
fmt.Println(data)
fmt.Println(string(data))
fmt.Printf("%s\n", data)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论