我有例子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字段的正确方法是什么?
在我的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) 我们可以通过这种方式在 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) 刚刚学习了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) 我正在尝试将 JSON 字节存储到 PostgreSQL,但存在问题。
\n\n\n\\u0000 无法转换为文本。
\n
如下所示,JSON 包含转义序列,例如\\u0000
,PostgreSQL 似乎将其解释为 unicode 字符,而不是 JSON 字符串。
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 当一组 goroutine 互相等待并且没有一个 goroutine 能够继续执行时,就会发生死锁。
例如:
func main() {
ch := make(chan int)
ch <- 1
fmt.Println(<-ch)
}
Run Code Online (Sandbox Code Playgroud)
但如果我们不使用通道,是否有可能发生死锁呢?
结果希望我有一个结构体
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)
如果存在,我怎样才能使它成为一个动态查询以显示年龄,否则它不会显示它?