小编nem*_*emo的帖子

在golang中逃避撇号

我如何在golang中逃避撇号?我有一个字符串

s = "I've this book"
Run Code Online (Sandbox Code Playgroud)

而且我想成功

s = "I\'ve this book"
Run Code Online (Sandbox Code Playgroud)

怎么做到这一点?提前致谢.

go

3
推荐指数
1
解决办法
5302
查看次数

通过电话后获得操作错误的惯用方法

如果我做

s, err := os.Stat(path)
Run Code Online (Sandbox Code Playgroud)

err != nil我需要知道,如果该文件不存在VS我没有权限来访问它,等我如何获得潜在的错误代码?阅读os包文档似乎建议我阅读错误字符串的文本 - 当然不是吗?

go

3
推荐指数
1
解决办法
896
查看次数

K&R第二版中的错误?

这对我来说是一个看起来像虫子的东西,但是我很困惑,考虑到这本书的年龄和受欢迎程度,我的观察结果似乎没有出现在互联网上的任何其他地方.或者也许我只是在搜索时很糟糕,或者根本不是一个错误.

我在谈论第一章中"打印出最长输入线"的程序.这是代码:

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print the longest input line */
main()
{
    int len; /* current line length */
    int max; /* maximum length seen so far */
    char line[MAXLINE]; /* current input line */
    char longest[MAXLINE]; /* longest line saved here */

    max = 0;
    while ((len = getline(line, MAXLINE)) > 0)
        if (len > max) {
            max = …
Run Code Online (Sandbox Code Playgroud)

c bounds

2
推荐指数
1
解决办法
205
查看次数

我怎么知道关闭是必要的?

在某些情况下,您需要关闭频道,有些情况则不需要.

http://play.golang.org/p/piJHpZ2-aU

queue := make(chan string, 2)
queue <- "one"
queue <- "two"
close(queue)

for elem := range queue {
    fmt.Println(elem)
}
Run Code Online (Sandbox Code Playgroud)

我来了

fatal error: all goroutines are asleep - deadlock!
Run Code Online (Sandbox Code Playgroud)

而此代码的关闭是可选的

http://play.golang.org/p/Os4T_rq0_B

jobs := make(chan int, 5)
done := make(chan bool)

go func() {
    for {
        j, more := <-jobs
        if more {
            fmt.Println("received job", j)
        } else {
            fmt.Println("received all jobs")
            done <- true
            return
        }
    }
}()

for j := 1; j <= 3; j++ {
    jobs …
Run Code Online (Sandbox Code Playgroud)

channel go

2
推荐指数
1
解决办法
144
查看次数

切片附加在渠道上

我想创建切片并添加从通道返回的值.下面是我尝试但无法解决的代码.

我必须发送切片的地址,但我无法弄清楚如何:(

package main

import "fmt"
import "time"

func sendvalues(cs chan int){
    for i:=0;i<10;i++{
        cs<-i   
    }   
}

func appendInt(cs chan int, aINt []int)[]*int{
    for {
        select {
        case i := <-cs:
            aINt = append(aINt,i)//append returns new type right ?
            fmt.Println("slice",aINt)
        }   
    }   
}



func main() {
    cs := make(chan int)
    intSlice := make([]int, 0,10)

    fmt.Println("Before",intSlice)
    go sendvalues(cs)
    go appendInt(cs,intSlice)// I have to pass address here

    time.Sleep(999*999999)
    fmt.Println("After",intSlice)
}
Run Code Online (Sandbox Code Playgroud)

go

2
推荐指数
1
解决办法
2626
查看次数

golang代码有什么问题

我想要杂草配对并计算它的频率

package main

import (
    "fmt"
)

type Pair struct {
    a int
    b int
}
type PairAndFreq struct {
    Pair
    Freq int
}

type PairSlice []PairAndFreq

type PairSliceSlice []PairSlice

func (pss PairSliceSlice) Weed() {
    fmt.Println(pss[0])
    weed(pss[0])
    fmt.Println(pss[0])
}

func weed(ps PairSlice) {
    m := make(map[Pair]int)

    for _, v := range ps {
        m[v.Pair]++
    }
    ps = ps[:0]
    for k, v := range m {

        ps = append(ps, PairAndFreq{k, v})

    }
    fmt.Println(ps)
}

func main() {
    pss := make(PairSliceSlice, 12) …
Run Code Online (Sandbox Code Playgroud)

go

2
推荐指数
1
解决办法
160
查看次数

无法将所有日志语句写入文件

我正在尝试为我的代码创建日志文件。但我无法将所有日志语句写入文件。

这是我的代码:

func MyLogger() {
    f, err := os.OpenFile("C:\\Project\\GoLang\\src\\Logs\\LogOutput\\TestLog.log", os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
    if err != nil {
        log.Fatalf("error opening file: %v", err)
    }
    defer f.Close()
    log.SetOutput(f)
    log.Println("This is a test log entry")
}
Run Code Online (Sandbox Code Playgroud)

go

2
推荐指数
1
解决办法
543
查看次数

使用新的和频道去

我在下面的技术演讲中找到了代码片段,我对一件事情有点困惑.

应该table <- new(Ball)放在之前go player("ping", table)
为什么我们甚至需要table <- new(Ball)?我认为table := make(chan *Ball)已经创造了渠道.这与死锁有关吗?

type Ball struct { hits int }

fun main() {
    table := make(chan *Ball)
    go player("ping", table)
    go player("pong", table)
    table <- new(Ball) // game on; toss the ball
    time.Sleep(1*time.Second)
    <-table // game over; grab the ball
}

func player(name string, table chan *Ball)
    for {
        ball := <-table
        ball.hits++
        fmt.println(name, ball.hits)
        time.Sleep(100 * time.Millisecond)
        table <- ball
    }
}
Run Code Online (Sandbox Code Playgroud)

go

2
推荐指数
1
解决办法
60
查看次数

Realloc()没有改变已经分配的内存的内存地址?

#include <stdio.h>
#include <stdlib.h>


void f(int **p) {
    printf("*p: %ptr\n", *p);

    *p = realloc(*p, 6*sizeof(int));

    printf("*p: %ptr\n", *p);
}


int main() {
    int *a = malloc(3*sizeof(int));
    printf("a before f(): %ptr", a);

    f(&a);
    printf("new a address after f(): %ptr\n", a);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,所有内存地址都是相同的.我错误地认为realloc()在地址上调用函数后它应该返回一个新的内存地址吗?

c

2
推荐指数
2
解决办法
156
查看次数

运行golang代码时出错,regex包丢失了吗?

我正在尝试运行一些我写过的go(golang)代码,并且我继续收到此错误:

cannot find package "regex" in any of:
    /usr/local/go/src/pkg/regex (from $GOROOT)
    /gopath/src/regex (from $GOPATH)
Run Code Online (Sandbox Code Playgroud)

当安装Go时,是否包含正则表达式?

如果没有,我可以在哪里找到它,我该如何安装它?

go

1
推荐指数
1
解决办法
233
查看次数

去线程死锁错误 - 使用go例程的正确方法是什么?

我正在编写一个程序,根据用户输入计算黎曼和.程序会将函数分成1000个矩形(是的,我知道我还没有得到那个数学)并总结它们并返回答案.我正在使用go例程计算1000个矩形,但我得到了一个

fatal error: all go routines are asleep - deadlock!
Run Code Online (Sandbox Code Playgroud)

处理多个go例程的正确方法是什么?我一直在环顾四周,还没有看到一个类似于我案例的例子?我是新手,想要遵守标准.这是我的代码(如果你想看看它的典型用例是什么,它是可以运行的 - 但它确实会破坏)

package main

import "fmt"
import "time"

//Data type to hold 'part' of function; ie. "4x^2"
type Pair struct {
    coef, exp int
}

//Calculates the y-value of a 'part' of the function and writes this to the channel
func calc(c *chan float32, p Pair, x float32) {
    val := x

    //Raise our x value to the power, contained in 'p'
    for i := 1; i < p.exp; …
Run Code Online (Sandbox Code Playgroud)

multithreading go

1
推荐指数
1
解决办法
55
查看次数

返回值作为逻辑OR的组合

我尝试找到它但找不到解决方案.

我只是想知道这对于回报价值意味着什么.这是否意味着返回值可以是存在的5个值中的任何一个.或者返回值都是这5个数字

static int is_navmsg(int msg)  
{  
          return msg==1019||msg==1020||msg==1044||msg==1045||msg==1046;  
}
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
539
查看次数

如果在go lang中有其他声明

有人可以帮我调试这个程序,只在每个输入上处理else部分.这是一个评分学生的程序.学生输入标记并显示成绩

func main(){
    var x int
    fmt.Println("Enter your marks")

    fmt.Scanf("%d",&x)

    if (100 <= x) && (x<=75){
        fmt.Println("D1")
    }else if (74 <= x)&&(x <= 70){
        fmt.Println("D2")
    }else if (69 <= x )&&(x<=65){
        fmt.Println("C3")
    }else if (64 <= x)&&(x <= 60){
        fmt.Println("C4")
    }else if (59 <= x)&&(x <= 55){
        fmt.Println("C5")
    }else if (54 <= x)&&( x<= 50){
        fmt.Println("C6")
    }else if (49 <= x )&&(x<= 45){
        fmt.Println("P7")
    }else{
        fmt.Println("Work harder")
    }
}
Run Code Online (Sandbox Code Playgroud)

if-statement go

-2
推荐指数
1
解决办法
5万
查看次数

标签 统计

go ×10

c ×3

bounds ×1

channel ×1

if-statement ×1

multithreading ×1