我想"directory/subdirectory/file.txt"在golang中打开一个给定的文件.以OS不可知的方式表示这种路径的推荐方法是什么(即Windows中的反斜杠,Mac和Linux中的正斜杠)?像Python的os.path模块?
作为一个愚蠢的基本线程练习,我一直在尝试在golang中实现睡眠理发师问题.有了频道,这应该很容易,但我遇到了一个heisenbug.也就是说,当我尝试诊断它时,问题就消失了!
考虑以下.该main()函数将整数(或"客户")推送到shop通道上.barber()读取shop频道以削减"顾客"的头发.如果我fmt.Print在customer()函数中插入一个语句,程序将按预期运行.否则,barber()永远不要削减任何人的头发.
package main
import "fmt"
func customer(id int, shop chan<- int) {
// Enter shop if seats available, otherwise leave
// fmt.Println("Uncomment this line and the program works")
if len(shop) < cap(shop) {
shop <- id
}
}
func barber(shop <-chan int) {
// Cut hair of anyone who enters the shop
for {
fmt.Println("Barber cuts hair of customer", <-shop)
}
}
func …Run Code Online (Sandbox Code Playgroud) 我正在尝试GLib.ThreadPool在Vala中使用s,但在搜索Google Code和现有文档后,我找不到任何好用的例子.我自己尝试使用它们导致未处理的GLib.ThreadErrors.
例如,考虑以下26行,它们是整数范围的乘法.
class Range {
public int low;
public int high;
public Range(int low, int high) {
this.low = low;
this.high = high;
}
}
void multiply_range(Range r) {
int product = 1;
for (int i=r.low; i<=r.high; i++)
product = product * i;
print("range(%s, %s) = %s\n",
r.low.to_string(), r.high.to_string(), product.to_string());
}
void main() {
ThreadPool<Range> threads;
threads = new ThreadPool<Range>((Func<Range>)multiply_range, 4, true);
for (int i=1; i<=10; i++)
threads.push(new Range(i, i+5));
}
Run Code Online (Sandbox Code Playgroud)
编译它们的 …