小编icz*_*cza的帖子

错误的值在if语句之外消失

我有以下代码:

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.

variables scope go

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

将 map[string]string 嵌入 Go JSON 编组中,无需额外的 JSON 属性(内联)

有没有办法嵌入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)

json marshalling go

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

此代码使用变量"ok"但未定义

这段代码正在运行,但我不明白如何.

在下面的代码中,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)

indexing dictionary go

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

为什么指针会产生错误“无效的间接”?

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)

arrays pointers go

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

golang是否有支持集数据结构的计划?

此功能可以使用'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是否有支持这种语法的计划?

dictionary set go

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

在 Golang 中绘制具有两个半径的圆圈

我环顾四周,但找不到任何对在 golang 中绘制圆圈有用的东西。我想绘制一个具有 2 个给定(内部和外部)半径的绘图,并为它们之间的所有像素着色。

一种可能的方法是遍历每个像素并为其着色,直到创建环。虽然,这似乎非常低效。

对此的任何帮助将不胜感激!:)

image draw go

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

为什么我的go coroutines在处理后被卡住了?

我是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)

loops channel go goroutine go-gorm

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

将十六进制字符串解析为图像/颜色

如何将Web颜色格式(3或6个十六进制数字)的RGB颜色解析为Colorfrom image/color?go是否有任何内置的解析器?我希望能够同时解析#XXXXXX#XXX颜色格式。 colordocs对此一无所知:https : //golang.org/pkg/image/color/,但是这个任务很常见,所以我相信go具有一些功能(我只是没有找到)。

string hex colors go

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

初始化自定义类型不适用于var,但适用于:=

我正在尝试初始化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)

variables types go

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

fmt.Sscanf 无法正确读取十六进制

我在读回已序列化为十六进制格式的值时遇到问题。当我格式化一个整数时,下面的代码产生一个 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)

string hex scanf go

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

标签 统计

go ×10

dictionary ×2

hex ×2

string ×2

variables ×2

arrays ×1

channel ×1

colors ×1

draw ×1

go-gorm ×1

goroutine ×1

image ×1

indexing ×1

json ×1

loops ×1

marshalling ×1

pointers ×1

scanf ×1

scope ×1

set ×1

types ×1