小编pet*_*rSO的帖子

关于Go语法的困惑

net在golang.org上的包源代码中看到了这一点.

c, err := dial(network, ra.toAddr(), dialer, d.deadline())
if d.KeepAlive > 0 && err == nil {
    if tc, ok := c.(*TCPConn); ok {
        tc.SetKeepAlive(true)
        tc.SetKeepAlivePeriod(d.KeepAlive)
        testHookSetKeepAlive()
    }
}
return c, err
Run Code Online (Sandbox Code Playgroud)

c.(*TCPConn)在这种情况下究竟做了什么?我认为最初它是某种类型的类型转换,但它返回2 tc和值ok.

这对我来说很困惑.有人能解释一下这段代码在做什么吗?

源代码在这里第171行.

go

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

解释这个Go语句

我发现这是在Go书中,并且无法在语言的语法中找到它.有谁能解释一下?它本质上是某种"标签"吗?

return (<-reply).(int)
Run Code Online (Sandbox Code Playgroud)

go

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

为什么Go认为我没有使用声明变量?

我是Go的新手,想知道为什么Go认为我没有使用vpcGrp?你会怎么编码?

这是我的代码:

var vpcGrp *ec2.SecurityGroup
grpExists := false
resp, err := svc.DescribeSecurityGroups(params)
if err != nil {
    log.Error(err)
} else {
    for _, v := range resp.SecurityGroups {
        if *v.GroupName == grpName {
            log.Debug("VPC Security Group Found [", grpName, "]")
            vpcGrp = v
            grpExists = true
        }
    }
}

if !grpExists {
    vpcGrp, err := createDbSecurityGrp(grpName, vpcId, region)
    if err != nil {
        log.Error(err)
        return nil, err
    }
}

return vpcGrp, nil
Run Code Online (Sandbox Code Playgroud)

go

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

time.Parse in Go,具有不同的小数秒长度

我正在从数据库中解析时间戳.我的布局如下:

layout = "2006-01-02 15:04:05.000000000 -0700 MST"

pipelineTS, err := time.Parse(layout, rawPipelineTS)
Run Code Online (Sandbox Code Playgroud)

问题是有时小数秒不是9位数,例如:

2018-12-18 15:25:08.73728596 +0000 UTC  
Run Code Online (Sandbox Code Playgroud)

当它找到这样的值时,它就会出错.有想法该怎么解决这个吗?时间戳来自DB表.我需要它取任意数量的小数位数.

time timestamp go datetime-parsing

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

Go fmt会自动缩进吗?

当我这样做fmt.Printf("...\n")时不会将光标移动到第0列,因此下一行是缩进的:

13
  13
    13
      13
        13
          13
            113 ('q')
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

package main

import (
    "bufio"
    "fmt"
    "os"
    "unicode"

    "golang.org/x/crypto/ssh/terminal"
)

func main() {
    oldState, err := terminal.MakeRaw(0)
    if err != nil {
        panic(err)
    }
    defer terminal.Restore(0, oldState)

    reader := bufio.NewReader(os.Stdin)

    var c rune
    for err == nil {
        if c == 'q' {
            break
        }

        c, _, err = reader.ReadRune()

        if unicode.IsControl(c) {
            fmt.Printf("%d\n", c)
        } else {
            fmt.Printf("%d ('%c')\n", c, c)
        }
    }

    if err != nil …
Run Code Online (Sandbox Code Playgroud)

unix terminal go

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

如何从此json_decode中提取值?

我使用api调用的json解码得到了这个结果.但我不知道如何从这个结果中提取"VALUE"..

$obj=json_decode($json_string);
print_r($obj);

stdClass Object ( [status] => OK [data] => stdClass Object ( [trends] => stdClass Object ( [rank] => Array ( [0] => stdClass Object ( [date] => 201011 [value] => 7196 ) ) ) [trends_low_sample] => [query_cost] => 1 [trends_frequency] => monthly ) ) 
Run Code Online (Sandbox Code Playgroud)

从这个结果我只需要"7196".我该怎么做呢??

php json

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

function()用作值编译错误

我正在尝试通过调整示例来学习Go的基础知识,因为我按照此处的教程进行操作:

http://tour.golang.org/#9


这是我写的一个小函数,只是将所有字符转换为所有大写字母.

package main

import (
    "fmt"
    "strings"
)

func capitalize(name string) {
    name = strings.ToTitle(name)
    return
}

func main() {
    test := "Sergio"    
    fmt.Println(capitalize(test))
}
Run Code Online (Sandbox Code Playgroud)

我得到了这个例外:

prog.go:15:用作值的大写(测试)

有什么明显的错误吗?

go

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

这个mmap系统调用有什么问题?

这是对mmap文件的尝试并写入一个字节:

package main

import (
  "fmt"
    "os"
    "syscall"
)

func main() {
    file, _ := os.Open("/tmp/data")
    mmap, _ := syscall.Mmap(int(file.Fd()), 0, 100, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
    fmt.Printf("cap is %d", cap(mmap))
    mmap[0] = 0
    syscall.Munmap(mmap)
}
Run Code Online (Sandbox Code Playgroud)

尽管长度设置为100,但mmap容量始终为0.系统调用出了什么问题?

go

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

Golang中的字符串转换和Unicode

我正在阅读Go Essentials

Go中的字符串是字节的不可变序列(8位字节值),这与Python,C#,Java或Swift等语言(其中字符串为Unicode)不同。

我在玩以下代码:

s := "???"
b :=[]byte{0xe6, 0x97, 0xa5, 0xe6, 0x9c, 0xac, 0xe8, 0xaa, 0x9e}
fmt.Println(string(b) == s) // true

for i, runeChar := range b {
    fmt.Printf("byte position %d: %#U\n", i, runeChar)
}

//byte position 0: U+00E6 'æ'
//byte position 1: U+0097
//byte position 2: U+00A5 '¥'
//byte position 3: U+00E6 'æ'
//byte position 4: U+009C
//byte position 5: U+00AC '¬'
//byte position 6: U+00E8 'è'
//byte position 7: U+00AA 'ª'
//byte position 8: U+009E …
Run Code Online (Sandbox Code Playgroud)

string unicode byte char go

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

从两个数组/切片中获取交集和排除项的最有效方法是什么?

给定两个数组或切片,例如:

a := []int{1, 2, 3, 4, 5}
b := []int{3, 4, 5, 6, 7, 8, 9}
Run Code Online (Sandbox Code Playgroud)

切片可能没有排序,顺序也没有关系。

什么是计算值的最有效方法,以使您最终获得两个切片的公共元素,而一个元素却不存在另一个元素的其余元素,即上面给出的两个数组的返回值将是:

common := []int{3, 4, 5}
inAButNotB := []int{1, 2}
inBButNotA := []int{6, 7, 8, 9}
Run Code Online (Sandbox Code Playgroud)

它很容易计算交叉点,将一个切片转换为地图,然后在一个切片上进行迭代以查看值是否存在。有没有一种方法可以计算同一循环中的其他两个值?

arrays go slice

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

标签 统计

go ×9

arrays ×1

byte ×1

char ×1

datetime-parsing ×1

json ×1

php ×1

slice ×1

string ×1

terminal ×1

time ×1

timestamp ×1

unicode ×1

unix ×1