小编mh-*_*bon的帖子

为 POST 请求创建一个 json 负载?

request, err := http.NewRequest("POST", url,bytes.NewBuffer(**myJsonPayload**))
Run Code Online (Sandbox Code Playgroud)

我是 Go 的新手,并尝试使用动态“ myJsonPayload ”发出发布请求,这将针对不同的请求而改变。

post json http go payload

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

当没有行时 RecordNotFound 返回 false

我遇到了这个库的问题,因为即使给定的输入不在数据库中,这个函数也会返回 false,而实际上它应该返回 true。

type User struct {
    ID          uint      `gorm:"primary_key"`
    Username    string    `json:",omitempty"`
    Password    string    `json:",omitempty"`
    CreatedAt   time.Time `json:",omitempty"`
}

b, err := db.Con()
if err != nil {
    log.Panic(err)
}

defer db.Close()

// We want an empty struct
// Otherwise it will trigger the unique key constraint
user := []User{}

// Check if the username is taken
// BUX, MUST FIX: This always returns false for some reason
if db.Where(&User{Username: "MyUsername"}).Find(&user).RecordNotFound() == false {
    fmt.Println("Username found")
} …
Run Code Online (Sandbox Code Playgroud)

go go-gorm

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

并发写入标准输出线程安全吗?

下面的代码不会引发数据竞争

package main

import (
    "fmt"
    "os"
    "strings"
)

func main() {
    x := strings.Repeat(" ", 1024)
    go func() {
        for {
            fmt.Fprintf(os.Stdout, x+"aa\n")
        }
    }()

    go func() {
        for {
            fmt.Fprintf(os.Stdout, x+"bb\n")
        }
    }()

    go func() {
        for {
            fmt.Fprintf(os.Stdout, x+"cc\n")
        }
    }()

    go func() {
        for {
            fmt.Fprintf(os.Stdout, x+"dd\n")
        }
    }()

    <-make(chan bool)
}
Run Code Online (Sandbox Code Playgroud)

我尝试了多种长度的数据,变体https://play.golang.org/p/29Cnwqj5K30

这篇文章说它不是TS。

这封邮件并没有真正回答问题,或者我没有理解。

osfmt 的包文档对此没有太多提及。我承认我没有挖掘这两个包的源代码来找到进一步的解释,它们对我来说太复杂了。

有哪些建议和参考

go

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

如何在 Google Colaboratory 中运行 Go 二进制文件

现在 Google Colaboratory 支持 Python2 和 Python3 内核。我可以添加 Go 内核以便我也可以在 Colab 中使用 Go 吗?

我在这里找到了一些例子,但它只有 JavaScript 和 R,没有 Go 语言。

kernel go jupyter-notebook google-colaboratory

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

json:无法将数字 5088060241 解组为 int 类型的结构

我正在使用 OVH 提供程序处理 Terraform 项目,当创建记录时,提供程序无法获取记录的 ID 并触发此错误: cannot unmarshal number 5088060240 into Go struct field OvhDomainZoneRecord.id of type int

我在 github 存储库上打开了一个问题,但仍在等待答案。我想自己解决这个问题,但我不是 Go 开发人员,我找不到任何相关的错误。

OvhDomainZoneRecord 的结构:

type OvhDomainZoneRecord struct {
    Id        int    `json:"id,omitempty"`
    Zone      string `json:"zone,omitempty"`
    Target    string `json:"target"`
    Ttl       int    `json:"ttl,omitempty"`
    FieldType string `json:"fieldType"`
    SubDomain string `json:"subDomain,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)

相关文件:https : //github.com/terraform-providers/terraform-provider-ovh/blob/master/ovh/resource_ovh_domain_zone_record.go

json integer go terraform

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

提取 avro 架构

类似于这个问题 How to extract schema for avro file in python 有没有办法在不事先知道架构的情况下读取golang中的avro文件并提取架构?

go avro

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

使用 Go 运行带参数的 bash 文件

我正在尝试运行具有如下参数的 bash 脚本:

./test.sh param1 param2
Run Code Online (Sandbox Code Playgroud)

bash文件

param1=$1
param2=$2

echo $param1
echo $param2
Run Code Online (Sandbox Code Playgroud)

但是它不起作用,但如果参数不存在,它将起作用。

cmd, _ := exec.Command("/bin/sh", fmt.Sprintf("./test.sh %s %s","test1","test2")).Output()
Run Code Online (Sandbox Code Playgroud)

但是,如果我更改 bash 脚本以执行其他操作而不将任何内容传递给它,那么它就可以工作。

cmd, _ := exec.Command("/bin/sh", "./test.sh").Output()
Run Code Online (Sandbox Code Playgroud)

如何使用 Go 将参数传递到 bash 文件中?

bash go

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

如何检查值是否动态实现接口?

假设我有一个名为的接口Hello

type Hello interface {
   Hi() string
}

Run Code Online (Sandbox Code Playgroud)

我想编写一个获取Hello和任何接口的函数,n并在Hello实现n接口的情况下执行某些操作,例如:

type Person interface {
  Name() int
}

type Animal interface {
  Leg() int
}

type hello struct{}

func (h hello) Hi() string {
    return "hello!"
}

func (h hello) Leg() int {
    return 4
}

func worker() {
   h := hello{}

  // Doesn't match
  check(h,(Person)(nil))

  // Matches
  check(h,(Animal)(nil))
}

func check(h Hello, n interface{}) {
  // of course this doesn't work, …
Run Code Online (Sandbox Code Playgroud)

reflection go

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

如何通过打开/关闭数据库用例(上下文管理)避免代码重复?

刚开始使用 Go,我想知道以下情况:

我有一个非常简单的代码库,我只想打开/关闭数据库连接并执行一个简单的查询。我可以按如下方式执行此操作(此处仅显示重要部分):

import (
    "database/sql"
    _ "github.com/lib/pq"
)

func (db *Database) ExecQueryA() {
    dbConn, err := sql.Open("postgres", db.psqlconn)
    if err != nil {
        panic(err)
    }
    defer dbConn.Close()

    _, err = db.Exec(...
    if err != nil {
        panic(err)
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的想法很好,但是如果我想再写 x 个这样的函数,我不想重复这部分:

import (
    "database/sql"
    _ "github.com/lib/pq"
)

func (db *Database) ExecQueryA() {
    dbConn, err := sql.Open("postgres", db.psqlconn)
    if err != nil {
        panic(err)
    }
    defer dbConn.Close()

    _, err = db.Exec(...
    if err != nil {
        panic(err)
    }
} …
Run Code Online (Sandbox Code Playgroud)

go

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

Go 接口比较令人困惑

为什么全局变量a1不等于局部变量a2,而a2等于a3和a4

我很困惑,a1,a2,a3都是通过调用getInterface方法计算的,但是比较它们时,却得到了不同的结果。

谢谢大家看到这个问题。


import "fmt"

var a1 aInterface = getInterface()

type aInterface interface {
    fun1()
}

type aInterfaceImpl struct{}

func (p *aInterfaceImpl) fun1() {
    return
}

func getInterface() aInterface {
    return new(aInterfaceImpl)
}

func main() {
    var a2 aInterface = getInterface()
    var a3 aInterface = getInterface()
    var a4 aInterface = new(aInterfaceImpl)
    fmt.Printf("a1 == a2, %+v\n", a1 == a2) // a1 == a2, false
    fmt.Printf("a2 == a3, %+v\n", a2 == a3) // a2 == a3, true
    fmt.Printf("a2 == a4, %+v\n", a2 …
Run Code Online (Sandbox Code Playgroud)

syntax go

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