小编mko*_*iva的帖子

如何修改golang中的struct字段

我有例子play.golang.org/p/Y1KX-t5Sj9我在struct User上定义方法Modify()

type User struct {
  Name string
  Age int
}
func (u *User) Modify() {
  *u = User{Name: "Paul"}
}
Run Code Online (Sandbox Code Playgroud)

在main()中我定义了struct literal &User {Name:"Leto",Age:11}然后调用u.Modify().这导致打印' 保罗0 '我喜欢结构字段名称已更改,但保持Age字段的正确方法是什么?

struct go

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

如何正确扫描Golang中array_agg函数的结果?

在我的Golang (1.15) 应用程序中,我使用sqlx包来处理PostgreSQL数据库 (PostgreSQL 12.5)。

我的SQL请求有一个array_agg函数,该函数返回字符串数组,如果为空则返回 null。

我正在尝试Scan此 SQL 请求的结果,但它在我的程序中引发了下一个错误:

sql:列索引 3 上扫描错误,名称“organization_ids”:不支持扫描,将 driver.Value 类型字符串存储到类型 *[]string

代码片段

type Channel struct {
    ChannelId           *string   `db:"channel_id" json:"channelId"`
    ChannelName         *string   `db:"channel_name" json:"channelName"`
    OrganizationsIds    *[]string `db:"organizations_ids" json:"organizationsIds"`
}

var channel Channel

row := db.QueryRow(`
    select
        channels.channel_id::text,
        channels.channel_name::text,
        array_agg(distinct channels_organizations_relationship.organization_id)::text[] organizations_ids
    from
        channels
    left join channels_organizations_relationship on
        channels.channel_id = channels_organizations_relationship.channel_id
    where
        channels.channel_id = $1
    group by
        channels.channel_id
    limit 1;`, *channelId)

if err := …
Run Code Online (Sandbox Code Playgroud)

sql postgresql go

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

在结构字段中使用指针的区别

我们可以通过这种方式在 golang 中创建结构体。下面的例子:这两者之间有什么区别?

// Usual way
type Employee struct {
    firstName string    `json:"name"`
    salary    int       `json:"salary"`
    fullTime  bool      `json:"fullTime"`
    projects  []Project `json:"projects"`
}

// Un-usal way with pointers
type Employee struct {
    firstName *string    `json:"name"`
    salary    *int       `json:"salary"`
    fullTime  *bool      `json:"fullTime"`
    projects  *[]Project `json:"projects"`
}
Run Code Online (Sandbox Code Playgroud)

有没有像内存这样的权衡?

更新:

假设以下函数:

// this function consumes MORE memory
func printEmployeeWithoutPointer(employee Employee) {
    // print here
}

// this function consumes LESS memory
func printEmployeeWithPointer(employee *Employee) {
    // print here
}
Run Code Online (Sandbox Code Playgroud)

struct pointers go

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

Go调度器什么时候会创建新的M和P?

刚刚学习了golang GMP模型,现在我了解了goroutines、操作系统线程和golang上下文/处理器如何相互协作。但我还是不明白什么时候会产生M和P?

例如,我有一个测试代码来在数据库上运行一些操作,并且有两个测试用例(两批 goroutine):

func Test_GMP(t *testing.T) {
    for _ = range []struct {
        name string
    }{
        {"first batch"},
        {"second batch"},
    } {
        goroutineSize := 50
        done := make(chan error, goroutineSize)
        for i := 0; i < goroutineSize; i++ {
            go func() {
                // do some databases operations...
                // each goroutine should be blocked here for some time...

                // propogate the result
                done <- nil
            }()
        }

        for i := 0; i < goroutineSize; i++ {
            select {
            case err …
Run Code Online (Sandbox Code Playgroud)

multithreading operating-system schedule go goroutine

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

如何使用 GORM 将包含转义码的 JSON 插入到 PostgreSQL 中的 JSONB 列中

我正在尝试将 JSON 字节存储到 PostgreSQL,但存在问题。

\n
\n

\\u0000 无法转换为文本。

\n
\n

如下所示,JSON 包含转义序列,例如\\u0000,PostgreSQL 似乎将其解释为 unicode 字符,而不是 JSON 字符串。

\n
err := raws.SaveRawData(data, url)\n// if there is "\\u0000" in the bytes\nif err.Error() == "ERROR: unsupported Unicode escape sequence (SQLSTATE 22P05)" {\n    // try to remove \\u0000, but not work\n    data = bytes.Trim(data, "\\u0000")\n    e := raws.SaveRawData(data, url) // save data again\n    if e != nil {\n        return e // return the same error\n    }\n    return nil\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Origin API 数据可以从这里 …

postgresql byte json go go-gorm

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

如果我们不使用通道,是否可能会发生死锁?

当一组 goroutine 互相等待并且没有一个 goroutine 能够继续执行时,就会发生死锁。

例如:

func main() {
        ch := make(chan int)
        ch <- 1
        fmt.Println(<-ch)
}
Run Code Online (Sandbox Code Playgroud)

但如果我们不使用通道,是否有可能发生死锁呢?

go

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

如果数据库golang mysql中的字段为空,则处理结果

结果希望我有一个结构体

type Users struct {
    ID    int    `json:"id"`
    Name  string `json:"name"`
    Age   string `json:"age"`
}
Run Code Online (Sandbox Code Playgroud)

我有一个 mysql 数据库,其中一些年龄值为零,因此基本上是为了使其动态,我一直在寻找解决方案。json:-如果它从 mysql 返回值 nil,则“年龄字符串”隐藏该字段。我做了两个查询

   query1: select id,name,age from users where age is not null
   query2: select id,name from users where age is null
Run Code Online (Sandbox Code Playgroud)

如果存在,我怎样才能使它成为一个动态查询以显示年龄,否则它不会显示它?

mysql struct go

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