有没有办法嵌入map[string]string内联?
我得到的是:
{
"title": "hello world",
"body": "this is a hello world post",
"tags": {
"hello": "world"
}
}
Run Code Online (Sandbox Code Playgroud)
我的意思是嵌入或内联是预期的结果,如下所示:
{
"title": "hello world",
"body": "this is a hello world post",
"hello": "world"
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码...我从 yaml 文件加载信息,并希望从上面返回所需格式的 JSON:
这是我的 yaml:
title: hello world
body: this is a hello world post
tags:
hello: world
Run Code Online (Sandbox Code Playgroud)
这是我的 Go 代码:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
)
type Post struct {
Title string `yaml:"title" json:"title"`
Body string `yaml:"body" json:"body"`
Tags …Run Code Online (Sandbox Code Playgroud) 这段代码正在运行,但我不明白如何.
在下面的代码中,hostProxy [host]可能包含也可能不包含函数.我不明白变量"ok"是如何定义的,或者它是如何获得它的值的.它没有在此行之前定义.
if fn, ok := hostProxy[host]; ok {
fn.ServeHTTP(w, r)
return
}
if target, ok := hostTarget[host]; ok {
....
}
Run Code Online (Sandbox Code Playgroud) p是一个指向数组的指针arr,我们可以arr通过 using获取数组*p,但是为什么不能通过 using 获取第二个元素*p[2]?
它会导致错误:
p[1] 的无效间接(int 类型)
以下代码:
arr := [4]int{1,2,3,4}
var p *[4]int = &arr
fmt.Println(p) // output &[1 2 3 4]
fmt.Println(*p) // output [1 2 3 4]
fmt.Println(p[1]) // output 2
fmt.Println(*p[1]) //generate an error:invalid indirect of p[1] (type int)
Run Code Online (Sandbox Code Playgroud) 此功能可以使用'map'实现.
countrySet := map[string]bool{
"US": true,
"JP": true,
"KR": true,
}
Run Code Online (Sandbox Code Playgroud)
但为了缓解读者的眼球,'set'是必要的数据结构.
countrySet := set[string]{"US", "JP", "KR"}
Run Code Online (Sandbox Code Playgroud)
或者仅使用键初始化'map'.例如:
countrySet := map[string]bool{"US", "JP", "KR"}
Run Code Online (Sandbox Code Playgroud)
golang是否有支持这种语法的计划?
我环顾四周,但找不到任何对在 golang 中绘制圆圈有用的东西。我想绘制一个具有 2 个给定(内部和外部)半径的绘图,并为它们之间的所有像素着色。
一种可能的方法是遍历每个像素并为其着色,直到创建环。虽然,这似乎非常低效。
对此的任何帮助将不胜感激!:)
我是Golang的新手.我一直在使用GORM和并发性去读取SQLite数据库并将其写入CSV文件.它工作顺利,但处理完成后,它不会结束主程序并退出.我必须打印command+c退出.我不知道我做错了什么.可能是它正在进入某种阻塞或死锁模式或其他什么.此外,它也不打印再见消息.这意味着它仍在尝试从频道中读取数据.请帮忙.这是代码.
package main
import (
"fmt"
"reflect"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type AirQuality struct {
// gorm.Model
// ID uint `gorm:"column:id"`
Index string `gorm:"column:index"`
BEN string `gorm:"column:BEN"`
CH4 string `gorm:"column:CH4"`
CO string `gorm:"column:CO"`
EBE string `gorm:"column:EBE"`
MXY string `gorm:"column:MXY"`
NMHC string `gorm:"column:NMHC"`
NO string `gorm:"column:NO"`
NO2 string `gorm:"column:NO_2"`
NOX string `gorm:"column:NOx"`
OXY string `gorm:"column:OXY"`
O3 string `gorm:"column:O_3"`
PM10 string `gorm:"column:PM10"`
PM25 string `gorm:"column:PM25"`
PXY string `gorm:"column:PXY"`
SO2 string `gorm:"column:SO_2"`
TCH string `gorm:"column:TCH"`
TOL string `gorm:"column:TOL"`
Time string `gorm:"column:date; …Run Code Online (Sandbox Code Playgroud) 我有这个基本的 go 程序,它打印到控制台并调用 2 个 goroutines
package main
import (
"fmt"
"time"
)
func f(from string) {
for i := 0; i < 3; i++ {
fmt.Println(from, ":", i)
}
}
func main() {
f("hello")
go f("foo")
go f("bar")
time.Sleep(time.Second)
}
Run Code Online (Sandbox Code Playgroud)
输出如下——我想知道为什么在“foo”之前打印“bar”——是什么决定了 goroutines 的执行顺序?
hello : 0
hello : 1
hello : 2
bar : 0
bar : 1
bar : 2
foo : 0
foo : 1
foo : 2
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 MongoDB 进行分页
我写这段代码:
findOptions := options.Find()
findOptions.SetLimit(20)
findOptions.SetSort(bson.M{{"_id", 1}})
cursor, err34 := collection.Find(context.Background(), bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions)
Run Code Online (Sandbox Code Playgroud)
现在它不断抱怨:
复合文字 go 中缺少类型并且映射文字 go 中缺少键
它抱怨这部分:
findOptions.SetSort(bson.M{{"_id", 1}})
Run Code Online (Sandbox Code Playgroud)
和
bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions)
Run Code Online (Sandbox Code Playgroud)
我已经被这个错误困扰了好几个小时了,这非常令人沮丧。
请帮忙 :(
我几天前刚刚开始学习 Go。今天,我们在调试一段代码一段时间时,发现了一些看起来与 Go 违反直觉的事情。
首先我们定义了一个接口和一个实现它的数据结构。
type Executer interface {
Execute()
}
type whatever struct {
name string
}
func (this *whatever) Execute() {
log.Println(this.name)
}
Run Code Online (Sandbox Code Playgroud)
现在考虑我有一个 nil 指针,whatever并且我尝试调用该方法Execute。在我迄今为止使用过的其他面向对象语言中,这会在调用方法(即w.Execute())时调用空指针错误,因为对象指针为空。有趣的是,在Go中,调用该方法时,Execute当我尝试取消引用时,该方法会发生空指针错误this.name。为什么不在调用该方法时呢?
func main() {
var w *whatever
w.Execute()
}
Run Code Online (Sandbox Code Playgroud)
所以,我现在想了解的是,这怎么可能?这是否意味着 Go 仅在编译时进行早期方法绑定,而在运行时没有方法与特定对象的绑定?
我正在比较两个切片,都是 type []int。一种是以json的形式进入API并解析为go struct的。在 struct 中,它被初始化为 empty []int{}。第二个保存在数据库(MongoDb)中,并被提取并映射到相同的结构类型。
在某些情况下,两个切片是完全空白的。但比较正在回归false。
reflect.DeepEqual(oldSettings.S1, newSettings.S1)
我还使用检查了两个字段类型 reflect.TypeOf(newSettings.S1). []int两者都在重新调整。
请考虑此游乐场链接以获取结构示例。
https://play.golang.org/p/1JTUCPImxwq
type OldSettings struct {
S1 []int
}
type NewSettings struct {
S1 []int
}
func main() {
oldSettings := OldSettings{}
newSettings := NewSettings{}
if reflect.DeepEqual(oldSettings.S1, newSettings.S1) == false {
fmt.Println("not equal")
} else {
fmt.Println("equal")
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!