小编Fer*_*sca的帖子

在Golang中使用长数字的JSON Marshaling给出了浮点数

我正在使用golang编组一个解组JSON,当我想用​​数字字段进行编码时,golang会以浮点数转换它,而不是使用长数字.

我有以下JSON:

{
    "id": 12423434, 
    "Name": "Fernando"
}
Run Code Online (Sandbox Code Playgroud)

将它封送到地图并再次解组为json字符串后,我得到:

{
    "id":1.2423434e+07,
    "Name":"Fernando"
}
Run Code Online (Sandbox Code Playgroud)

如您所见,"id"字段采用浮点表示法.

我正在使用的代码如下:

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

func main() {

    //Create the Json string
    var b = []byte(`
        {
        "id": 12423434, 
        "Name": "Fernando"
        }
    `)

    //Marshal the json to a map
    var f interface{}
    json.Unmarshal(b, &f)
    m := f.(map[string]interface{})

    //print the map
    fmt.Println(m)

    //unmarshal the map to json
    result,_:= json.Marshal(m)

    //print the json
    os.Stdout.Write(result)

}
Run Code Online (Sandbox Code Playgroud)

它打印:map [id:1.2423434e + 07姓名:Fernando] {"姓名":"Fernando","id":1.2423434e + 07}

似乎地图的第一个元帅会产生FP.我该如何修复它?

这是goland游乐场的节目链接: …

floating-point json marshalling go

51
推荐指数
2
解决办法
3万
查看次数

如何用go编程语言处理并行HTTP请求?

我正在使用go HTTP包.我想像在java中那样并行处理请求.但我不能.

我创建了一个简单的Web服务器,在中间休眠,并意识到每次处理一个请求,所以如果我在浏览器上刷新,第一个请求的过程必须完成,直到第二个请求开始处理,这里是代码:

func main(){

    //Process the http commands
    fmt.Printf("Starting http Server ... ")
    http.Handle("/", http.HandlerFunc(sayHello))
    err := http.ListenAndServe("0.0.0.0:8080", nil)
    if err != nil {
        fmt.Printf("ListenAndServe Error",err)
    }
}

func sayHello(c http.ResponseWriter, req *http.Request) {
    fmt.Printf("New Request\n")
    processRequest(c, req)
}

func processRequest(w http.ResponseWriter, req *http.Request){
    time.Sleep(time.Second*3)
    w.Write([]byte("Go Say’s Hello(Via http)"))
    fmt.Println("End")
}
Run Code Online (Sandbox Code Playgroud)

由于我想并行处理两个请求,我在"sayHello"函数中的"processRequest(c,req)"之前添加了"go"命令,以便处理不同gorutine中的每个请求.但是......它不起作用......我不知道为什么.我知道这两个请求都已处理,因为我在控制台看到了打印的行,但浏览器一直在等待信息.....并且没有显示我的回复.

那么......我的问题,

每个请求都会创建一个新的http.ResponseWriter吗?或它使用相同?您知道如何指示Web服务器使用不同的线程处理每个请求吗?

欢迎任何帮助....

Fersca

go

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

无法得到golang pprof工作

我试图描述一些golang应用程序,但我没有那个工作,我已经按照这两个教程:

两者都说在向应用程序添加一些代码行后,你必须执行你的应用程序,我这样做了,我在屏幕上收到以下消息:

2015/06/16 12:04:00个人资料:cpu profiling enabled,/ var/folder/kg/4fxym1sn0bx02zl_2sdbmrhr9wjvqt/T /profile680799962/cpu.pprof

所以,我知道正在执行分析,将信息发送到文件.

但是,当我看到文件大小时,在我测试的任何程序中,它总是64字节.

当我尝试用pprof打开cpu.pprof文件时,我执行"top10"命令,我发现文件中没有任何内容:

("./fact"是我的应用)

go tool pprof ./fact /var/folders/kg/4fxym1sn0bx02zl_2sdbmrhr9wjvqt/T/profile680799962/cpu.pprof

top10 - >

(pprof)top10 0 of 0 total(0%)flat flat%sum%cum cum%

所以,当我分析时,就好像没有任何事情发生.

我已经在mac(本例)和ubuntu中测试了它,有三个不同的程序.

你知道我做错了吗?

然后示例程序非常简单,这是代码(是一个非常简单的因子程序,我从互联网上):

import "fmt"
import "github.com/davecheney/profile"

func fact(n int) int {
      if n == 0 {
        return 1
      }
      return n * fact(n-1)
}

func main() {
      defer profile.Start(profile.CPUProfile).Stop()
      fmt.Println(fact(30))
}
Run Code Online (Sandbox Code Playgroud)

谢谢,Fer

profiling go pprof

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

标签 统计

go ×3

floating-point ×1

json ×1

marshalling ×1

pprof ×1

profiling ×1