我在读取YAML文件时遇到问题,YAML文件包含Unicode字符转义.但是当我加载YAML文件并打印时fileInfo,包含Unicode字符转义(例如'a\u0000b')的字符串在使用unMarshal()函数时被转义.
这是我的YAML文件(conf.yml):
topicList:
- source: 'test'
target: 'temp'
- source: 'a\u0000b'
target: 'temp'
Run Code Online (Sandbox Code Playgroud)
我的代码是:
import (
"fmt"
"io/ioutil"
"strings"
"gopkg.in/yaml.v2"
)
type Config struct {
TopicList []Topic `yaml:"topicList"`
}
type Topic struct {
Source string `yaml:"source" json:"source"`
Target string `yaml:"target" json:"target"`
}
func main() {
cfg, err := NewConfig("conf.yml")
if err != nil {
fmt.Println("load config fail: ", err)
}
for _, s := range cfg.TopicList {
fmt.Println("\n +++++ sourceTopic = ", …Run Code Online (Sandbox Code Playgroud) 我是新来的,有点混淆设置饼干jar gloabally.我正在使用http包中的cookiejar,这是我在http.Client中将cookie设置为jar时可用的其他文档的实现.
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
req, _ := http.NewRequest("GET", request_url, nil)
q := req.URL.Query()
q.Add("authtoken", token)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.URL.RawQuery = q.Encode()
res, _ := client.Do(req)
defer res.Body.Close()
fmt.Println(res.Cookies()) // can see the cookies here
Run Code Online (Sandbox Code Playgroud)
我在这里尝试实现的是将其全局声明,以便在设置jar后,后续客户端请求将具有cookie.如果我将它放在任何其他函数中,它将再次设置为nil.
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
Run Code Online (Sandbox Code Playgroud)
如何做到这一点的最佳实践?谢谢.
我有以下代码:
if err == nil {
body, err := ioutil.ReadAll(response.Body)
if err == nil {
dataMap := &models.UserResponse{}
json.Unmarshal(body, &dataMap)
if dataMap.User == (models.UserId{}) {
err = fmt.Errorf("unauthorized")
fmt.Println(err) // when unathorized, prints unauthorized
}
}
}
fmt.Println(err) // always prints nil
Run Code Online (Sandbox Code Playgroud)
该Println内部if dataMap.User ...打印"unauthorized",而最后Println总是打印nil.
我不知道它为什么会发生,在这个函数的开头err声明var err error.
有没有办法嵌入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 代码 https://www.intervue.io/sandbox-ILSCXZ6RR
func worker() chan int {
ch := make(chan int)
go func() {
time.Sleep(3 * time.Second)
ch <- 42
}()
return ch
}
func main() {
timeStart := time.Now()
_, _ = <-worker(), <-worker()
println(int(time.Since(timeStart).Seconds())) // 3 or 6 ?
}
Run Code Online (Sandbox Code Playgroud)
如何在 3 秒内执行而不是在 6 秒内执行?