我有以下代码:
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) 我正在尝试初始化ErrNegativeSqrt,它是一个自定义的float64类型,但是如果我这样做,var它将无法正常工作。
看一下func Sqrt(x float64) (float64, error):
package main
import (
"fmt"
)
type ErrNegativeSqrt float64
func (e *ErrNegativeSqrt) Error() string {
return fmt.Sprint("cannot Sqrt negative number: %f", float64(*e))
}
func Sqrt(x float64) (float64, error) {
if x < 0 {
var err ErrNegativeSqrt = x
// This works: err := ErrNegativeSqrt(x)
return x, &err
}
z := x / 2
i := 1
for prev_z := 0.0; z != prev_z && Abs(z-prev_z) > 0.000000000000001; i++ { …Run Code Online (Sandbox Code Playgroud) 我在读回已序列化为十六进制格式的值时遇到问题。当我格式化一个整数时,下面的代码产生一个 0x14 的值。但是,当我尝试从字符串中读取该值时,得到的结果无效。有人可以帮我弄清楚我做错了什么吗?
我有预先存在的文本文件,我正在用包含这种格式的多行进行解析,因此序列化为不同的格式将不是一个可行的解决方案。我需要使用这种特定格式。
根据 go 文档,这应该有效:https : //golang.org/pkg/fmt/
动词的行为类似于 Printf 的动词。例如,%x 将扫描一个整数作为十六进制数,而 %v 将扫描该值的默认表示格式。Printf 动词 %p 和 %T 以及标志 # 和 + 未实现。对于浮点和复数值,所有有效的格式化动词(%b %e %E %f %F %g %G %x %X 和 %v)都是等价的,并且接受十进制和十六进制表示法(例如:“2.3 e+7", "0x4.5p-8") 和数字分隔下划线(例如:“3.14159_26535_89793”)。
package main
import (
"fmt"
)
func main() {
encode := 20
fmt.Println(fmt.Sprintf("%#x", encode)) // 0x14
var decode int
numRead, err := fmt.Sscanf("0x14", "%#x", &decode)
fmt.Println(decode, numRead, err) // 0 1 bad verb '%#' for integer
numRead, err = fmt.Sscanf("0x14", "%x", &decode) …Run Code Online (Sandbox Code Playgroud)