我最近开始使用go,我对这个程序的执行顺序感到很困惑.我希望我不是在这里问一些非常微不足道的问题.
这在golang之旅基本上是#69,我插入了一些Println; 链接到游乐场:http://play.golang.org/p/PXDlD3EA2f
func fibonacci(c, quit chan int) {
x, y := 0, 1
fmt.Println("Inside the fibonacci")
for {
select {
case c <- x:
fmt.Println("Inside the for, first case, before reassigning ", x, y)
x, y = y, x+y
fmt.Println("Inside the for, first case, after reassigning ", x, y)
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
fmt.Println("Begin of Main")
c := make(chan int)
quit := make(chan int)
fmt.Println("Before gonig to the func")
go …Run Code Online (Sandbox Code Playgroud) 我想使用 Go 检索具有特定 id 的所有标签。显然,最简单的方法就是使用 goquery。
假设我正在网站中寻找ul带有 id 的标签。我想列出这样一个 .txt 文件中包含的所有内容。我以前从未使用过 jQuery,所以我感觉有点失落。MyTagsiteliul
resp, _ := http.Get(site)
httpBody := resp.Body
node, _ := html.Parse(httpBody)
document := goquery.NewDocumentFromNode(node)
document.Find("ul.MyTag").Each(func(i int, ul *goquery.Selection) { //MyTag will not work here
ul.Find("li").Each(func (i int, li *goquery.Selection){
...
})
})
Run Code Online (Sandbox Code Playgroud)
更具体地说,我的 html 看起来像
<html>
<body>
<ui id="yes">
<li key="1">a</li>
<li key="2">b</li>
<li key="3">c</li>
<li key="4">d</li>
</ui>
<ui id="no">
<li key="1">11</li>
<li key="2">22</li>
<li key="3">33</li>
<li key="4">44</li>
</ui>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我想取回钥匙 …
我理解的>>=术语定义join
xs >>= f = join (fmap f xs)
Run Code Online (Sandbox Code Playgroud)
这也告诉我们fmap + join收益率>>=
我想知道对于Listmonad是否可以定义没有join,就像我们为例如Maybe:
>>= m f = case m of
Nothing -> Nothing
Just x -> f x
Run Code Online (Sandbox Code Playgroud) 我有一个列表,其中包含一个弹出元素的函数,以及另一个"接收"弹出元素的函数.我认为接收器关闭后会关闭通道,但似乎该程序在到达之前已经死锁.这是最好的方法吗?我应该有另一个通道来检测弹出窗口何时完成?
func pop(list *[]int, c chan int) {
if len(*list) != 0 {
result := (*list)[0]
*list = (*list)[1:]
fmt.Println("about to send ", result)
c <- result
} else {
return
}
}
func receiver(c chan int) {
result := <-c
fmt.Println("received ", result)
}
var list = []int{1, 2, 3}
func main() {
fmt.Println("Main")
c := make(chan int)
go pop(&list, c)
go pop(&list, c)
for len(list) > 0 {
receiver(c)
}
close(c) //Dosen't seem to have any effect …Run Code Online (Sandbox Code Playgroud)