小编jor*_*175的帖子

何时使用Golang的默认MUX而不是自己做

我已经看到很多帖子谈到在Go中构建自己的MUX,其中一个例子就在这里(http://thenewstack.io/building-a-web-server-in-go/).

什么时候应该使用默认值而不是定义自己的?Go文档和博客文章都没有说明为什么你应该使用一个而不是另一个.

http go mux

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

在 Golang 中解码任意 JSON

我有一个关于在 Go 中解码任意 JSON 对象/消息的问题。假设您有三个截然不同的 JSON 对象(又名消息),您可以在 http 连接上接收它们,为了说明起见,我们称它们为:

{ home : { some unique set of arrays, objects, fields, and arrays objects } }
Run Code Online (Sandbox Code Playgroud)

{ bike : { some unique set of arrays, objects, fields, and arrays objects } }
Run Code Online (Sandbox Code Playgroud)

{ soda : { some unique set of arrays, objects, fields, and arrays objects } }
Run Code Online (Sandbox Code Playgroud)

我的想法是你可以将它们从 http 连接解码为接口映射,例如:

func httpServerHandler(w http.ResponseWriter, r *http.Request) {
    message := make(map[string]interface{})
    decoder := json.NewDecoder(r.Body)
    _ = decoder.Decode(&message)
Run Code Online (Sandbox Code Playgroud)

然后执行 if, else …

json dictionary interface go unmarshalling

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

使用与Golang Maps和Structs以及JSON的接口

我有一些JSON代码,看起来像:

{
    "message_id": "12345",
    "status_type": "ERROR",
    "status": {
        "x-value": "foo1234",
        "y-value": "bar4321"
    }
}
Run Code Online (Sandbox Code Playgroud)

或者看起来像这样.正如您所看到的,"status"元素基于status_type从字符串的标准对象更改为字符串数组的对象.

{
    "message_id": "12345",
    "status_type": "VALID",
    "status": {
        "site-value": [
            "site1",
            "site2"
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我想我需要让我的"状态"结构采用像"map [string] interface {}"这样的地图,但我不确定该怎么做.

你也可以在操场上看到这里的代码.
http://play.golang.org/p/wKowJu_lng

package main

import (
    "encoding/json"
    "fmt"
)

type StatusType struct {
    Id     string            `json:"message_id,omitempty"`
    Status map[string]string `json:"status,omitempty"`
}

func main() {
    var s StatusType
    s.Id = "12345"
    m := make(map[string]string)
    s.Status = m
    s.Status["x-value"] = "foo1234"
    s.Status["y-value"] = "bar4321"

    var data []byte
    data, _ = …
Run Code Online (Sandbox Code Playgroud)

json dictionary interface go

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

如何将Go中的http.Client绑定到IP地址

我有一台带有多个网卡的客户端计算机,如何将Go中的http.Client绑定到某个网卡或某个SRC IP地址?

假设您有一些非常基本的http客户端代码,如下所示:

package main

import (
    "net/http"
)

func main() {
    webclient := &http.Client{}
    req, _ := http.NewRequest("GET", "http://www.google.com", nil)
    httpResponse, _ := webclient.Do(req)
    defer httpResponse.Body.Close()
}
Run Code Online (Sandbox Code Playgroud)

有没有办法绑定到某个NIC或IP?

ip http go

4
推荐指数
2
解决办法
3526
查看次数

如何为Go地图指定指针

我在分配指向地图的指针时遇到问题.也许这是Go中的一个错误?或许我只是做错了什么.代码也在操场上,https://play.golang.org/p/p0NosPtkptz

这是一些说明问题的超简化代码.我正在创建一个名为collections的对象,其中包含两个集合对象.然后我循环遍历这些集合并将它们分配给地图,其中地图中的键是集合ID.

package main

import (
    "fmt"
)

type collection struct {
    ID string
    Name string
}

type collections struct {
    Collections []collection
}

type cache struct {
    Index int
    Collections map[string]*collection
}

func main() {
    var c cache
    c.Collections = make(map[string]*collection)

    // Create 2 Collections
    var col1, col2 collection
    col1.ID = "aa"
    col1.Name = "Text A"
    col2.ID = "bb"
    col2.Name = "Test B"

    // Add to Collections
    var cols collections
    cols.Collections = append(cols.Collections, col1)
    cols.Collections = append(cols.Collections, col2) …
Run Code Online (Sandbox Code Playgroud)

dictionary pointers go

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

标签 统计

go ×5

dictionary ×3

http ×2

interface ×2

json ×2

ip ×1

mux ×1

pointers ×1

unmarshalling ×1