英文:
How to implement the query function
问题
我尝试开发一个程序,可以同时从多个复制的数据库中读取数据。它必须接受最先到达的响应。所以任务是实现query
函数。我尝试使用select
,但程序报错,我觉得我走错了方向。你能告诉我如何正确实现这个程序吗?
package main
import (
"fmt"
"time"
)
type Conn interface {
DoQuery(query string) Result
}
type Result struct {
Query string
ConnName string
}
type Conn1 struct{}
type Conn2 struct{}
type Conn3 struct{}
func (c Conn1) DoQuery(query string) Result {
time.Sleep(1 * time.Second)
return Result{Query: query, ConnName: "Conn1"}
}
func (c Conn2) DoQuery(query string) Result {
time.Sleep(2 * time.Second)
return Result{Query: query, ConnName: "Conn2"}
}
func (c Conn3) DoQuery(query string) Result {
time.Sleep(3 * time.Second)
return Result{Query: query, ConnName: "Conn3"}
}
func query(conns []Conn, query string) Result {
// <- 这里是我实现的代码的开始部分
ch1 := make(chan Conn1)
ch2 := make(chan Conn2)
ch3 := make(chan Conn3)
select {
case one := <-ch1:
return one.DoQuery("First query")
case two := <-ch2:
return two.DoQuery("Second query")
case three := <-ch3:
return three.DoQuery("Third query")
}
// -> 这里是代码的结束部分
}
func main() {
conns := make([]Conn, 3)
conns[0] = Conn1{}
conns[1] = Conn2{}
conns[2] = Conn3{}
res := query(conns, "select * from users")
fmt.Println(res)
}
你的代码中,使用了select
语句来同时等待多个通道的结果。但是你定义的通道类型是chan Conn1
、chan Conn2
和chan Conn3
,而不是chan Conn
。因此,你需要将通道类型修改为chan Conn
,并在调用DoQuery
方法时传递正确的查询语句。
修改后的代码如下:
package main
import (
"fmt"
"time"
)
type Conn interface {
DoQuery(query string) Result
}
type Result struct {
Query string
ConnName string
}
type Conn1 struct{}
type Conn2 struct{}
type Conn3 struct{}
func (c Conn1) DoQuery(query string) Result {
time.Sleep(1 * time.Second)
return Result{Query: query, ConnName: "Conn1"}
}
func (c Conn2) DoQuery(query string) Result {
time.Sleep(2 * time.Second)
return Result{Query: query, ConnName: "Conn2"}
}
func (c Conn3) DoQuery(query string) Result {
time.Sleep(3 * time.Second)
return Result{Query: query, ConnName: "Conn3"}
}
func query(conns []Conn, query string) Result {
ch1 := make(chan Conn)
ch2 := make(chan Conn)
ch3 := make(chan Conn)
go func() {
ch1 <- conns[0]
}()
go func() {
ch2 <- conns[1]
}()
go func() {
ch3 <- conns[2]
}()
select {
case one := <-ch1:
return one.DoQuery(query)
case two := <-ch2:
return two.DoQuery(query)
case three := <-ch3:
return three.DoQuery(query)
}
}
func main() {
conns := make([]Conn, 3)
conns[0] = Conn1{}
conns[1] = Conn2{}
conns[2] = Conn3{}
res := query(conns, "select * from users")
fmt.Println(res)
}
这样修改后,程序会同时向三个通道发送请求,并等待最先到达的响应。然后返回该响应的结果。
英文:
I try to develop a program that reads from multiple replicated databases simultaneously. It must accept the response that arrives first. So the tsk is to implement the query
function. I tried select
, but the program gives an error and I think I'm going in the wrong direction. Can you tell me how to properly implement this program.
package main
import (
"fmt"
"time"
)
type Conn interface {
DoQuery(query string) Result
}
type Result struct {
Query string
ConnName string
}
type Conn1 struct{}
type Conn2 struct{}
type Conn3 struct{}
func (c Conn1) DoQuery(query string) Result {
time.Sleep(1 * time.Second)
return Result{Query: query, ConnName: "Conn1"}
}
func (c Conn2) DoQuery(query string) Result {
time.Sleep(2 * time.Second)
return Result{Query: query, ConnName: "Conn2"}
}
func (c Conn3) DoQuery(query string) Result {
time.Sleep(3 * time.Second)
return Result{Query: query, ConnName: "Conn3"}
}
func query(conns []Conn, query string) Result {
// <- here is the beginning of code that I implemented
ch1 := make(chan Conn1)
ch2 := make(chan Conn2)
ch3 := make(chan Conn3)
select {
case one := <-ch1:
return one.DoQuery("First query")
case two := <-ch2:
return two.DoQuery("Second query")
case three := <-ch3:
return three.DoQuery("Third query")
}
// -> here is the end of code
}
func main() {
conns := make([]Conn, 3)
conns[0] = Conn1{}
conns[1] = Conn2{}
conns[2] = Conn3{}
res := query(conns, "select * from users")
fmt.Println(res)
}
答案1
得分: 1
在每个goroutine中运行每个查询,并将每个查询的结果发送到一个单一的通道中。在主goroutine中接收通道以获取第一个结果。有关更多信息,请参见注释。
func query(conns []Conn, query string) Result {
// 创建接收第一个结果的通道。容量len(conns)-1确保所有goroutine都可以向通道发送值并退出。
ch := make(chan Result, len(conns)-1)
// 在每个连接上启动一个goroutine进行查询。
// https://go.dev/doc/faq#closures_and_goroutines 解释了为什么将conn作为参数传递。
for _, conn := range conns {
go func(conn Conn) {
ch <- conn.DoQuery(query)
}(conn)
}
// 等待第一个结果并返回它。
return <-ch
}
英文:
Run each query in a goroutine and send the result of each query to a single channel. Receive on the channel in the main goroutine to get the first result. See the commentary for more info.
func query(conns []Conn, query string) Result {
// Create the channel to receive the first result. The capacity
// len(conns)-1 ensures that all goroutines can send a value to the
// channel and exit.
ch := make(chan Result, len(conns)-1)
// Start a goroutine to query on each connection.
// https://go.dev/doc/faq#closures_and_goroutines explains
// why conn is passed as an argument.
for _, conn := range conns {
go func(conn Conn) {
ch <- conn.DoQuery(query)
}(conn)
}
// Wait for the the first result and return it.
return <-ch
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论