小编Fli*_*mzy的帖子

推迟当前值

我想使用 defer 和函数参数中参数的最新值来调用该函数。我怀疑运行这段代码:

package main

import (
    "fmt"
)

func main() {
    s := "ABC"
    
    defer fmt.Println(s)
    s = "DEF"
}
Run Code Online (Sandbox Code Playgroud)

我会得到DEF。但我得到的是ABC。有什么办法可以得到DEF吗?

go

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

如何使用数组正确格式化 JSON

我正在尝试在我的 POST 请求中发送一个 JSON 有效负载,但我不确定如何正确格式化它以使用数组。下面是正确的 JSON本身的样子:

{
    "animal": "dog",
    "contents": [{
        "name": "daisy",
        "VAL": "234.92133",
        "age": 3
    }]
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我有这个:

        group := map[string]interface{}{
            "animal": "dog",
            "contents": map[string]interface{}{
                "name": "daisy",
                "VAL":  "234.92133",
                "age":  3,
            },
        }
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何处理内容数组(方括号),只有“内容”中的大括号。

arrays json http request go

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

如果第一个失败,Go 是否执行“if”中的所有条件?

想象一下下面的一段代码:

if someBool && funcReturningABool() {
    // code here...
}
Run Code Online (Sandbox Code Playgroud)

wheresomeBool是一个布尔值并funcReturningABool返回真或假。

如果someBool等于false,funcReturningABool仍然会被执行吗?

if-statement execution go

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

Go 如何打开文件?

我使用 Iris 版本 12 构建了一个 Go Web 项目,现在有一个名为 的文件config.go,我可以在包含 的文件夹中config.json通过 shell 脚本输出,但是请注意. 文件夹的结构如下:cat ../config.jsonconfig.gopanic: open ../config.json: no such file or directory

.
??? config
?   ??? config.go
??? config.json
??? config.yml
??? controller
??? datasource
??? go.mod
??? go.sum
??? main.go
??? model
?   ??? user.go
??? service
??? static
?   ??? css
?   ?   ??? app.85873a69abe58e3fc37a13d571ef59e2.css
?   ??? favicons
?   ?   ??? favicon.ico
?   ??? fonts
?   ?   ??? element-icons.b02bdc1.ttf
?   ??? …
Run Code Online (Sandbox Code Playgroud)

go

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

在选择 Ticker 时,time.After 不起作用

我在尝试时遇到了这个问题time.Ticker

如下面的代码,case <-time.After(time.Millisecond * 5500):从不执行,程序重复打印“ticker”和“chan”。

谁能解释一下吗?

package main

import (
    "fmt"
    "os"
    "time"
)

func main() {
    ticker := time.NewTicker(time.Second * 2)
    defer ticker.Stop()
    ch := time.Tick(time.Second * 3)
    for {
        select {
        case t := <-ticker.C:
            fmt.Println("From ticker:", t)
        case t := <-ch:
            fmt.Println("From chan:", t)
        case <-time.After(time.Millisecond * 5500):
            fmt.Println("timeout, exit")
            os.Exit(1)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

From ticker: 2021-02-02 11:31:55.9902354 +0800 CST m=+2.002391201
From chan: 2021-02-02 11:31:56.990678 +0800 CST m=+3.002835701
From ticker: 2021-02-02 11:31:57.9900811 …
Run Code Online (Sandbox Code Playgroud)

go

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

为什么 go-lint 会给出关于首字母缩写的不一致建议?

go-lint 建议如下:

method CreateStaticCssPath should be CreateStaticCSSPath
Run Code Online (Sandbox Code Playgroud)

linter 是否正确,如果正确,为什么?

它允许以前的方法:

CreateStaticJsPath
Run Code Online (Sandbox Code Playgroud)

go golint

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

如何进行并行 http.Get 查询

我正在尝试并行进行 http 查询,然后等待所有查询完成:

  g1, _ := go http.Get('http://example.com/1')
  g2, _ := go http.Get('http://example.com/2')

  res1 := g1.wait() or { panic(err) }
  res2 := g2.wait() or { panic(err) }

  data1 := json.decode(ApiResult, res1.text) or { panic(err) }
  data2 := json.decode(ApiResult, res2.text) or { panic(err) }
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

src/main.go:159:11: syntax error: unexpected go, expecting expression
Run Code Online (Sandbox Code Playgroud)

parallel-processing go go-http

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

不能调用非函数withdraw (type int) 是什么意思?

我创建了这个程序

package main

import "fmt"

var bankAccount = 12342

func withdraw(withdrawAmnt int) int { //withdraw function, return int
    bankAccount -= withdrawAmnt
    if bankAccount < 0 {
        bankAccount += withdrawAmnt //bankaccount cannot go negative
        return 1
    } else { return 0 }
}

func deposit(depositAmnt int) {
    bankAccount += depositAmnt
}

func main() {
    var choice int
    var withdraw int
    var deposit int
    fmt.Printf("%d \n 1. withdraw \n 2. deposit \n 3. exit")
    fmt.Scanln(&choice)
    switch choice {
        case 1:
            fmt.Scanln(&withdraw)
            if …
Run Code Online (Sandbox Code Playgroud)

go

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

有没有办法计算Go应用程序的启动时间?

我尝试计算 Go 应用程序启动和接受请求所需的时间。我尝试使用以下方法来做到这一点:

func main() {
    start:=time.Now()
    repo.CreateConnection()
    router := mux.NewRouter()
    r := bookController.Controller(router)
    fmt.Println("Starting server on the port 8080...")
    log.Fatal(http.ListenAndServe(":8080", r))
    fmt.Println(time.Since(start))
}
Run Code Online (Sandbox Code Playgroud)

但控制台只打印直到 Starting server on the port 8080...

GOROOT=/usr/local/go #gosetup
GOPATH=/Users/admin/go #gosetup
/usr/local/go/bin/go build -o /private/var/folders/t3/1fk2w7y55qs1dfxbxbvpsn9h0000gp/T/___go_build_payments_go_performance go-performance #gosetup
/private/var/folders/t3/1fk2w7y55qs1dfxbxbvpsn9h0000gp/T/___go_build_go_performance
Successfully connected!
Starting server on the port 8080...
Run Code Online (Sandbox Code Playgroud)

有没有办法显示正确的启动时间?我所说的启动时间是指该应用程序开始侦听端口 8080 所需的时间。

testing performance go performance-testing

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

将用户的24小时输入转换为12小时

因此,出于某种原因,这会出现一条错误消息,指出'方法'没有重载'抛出'参数'需要1个参数'.有谁知道我做错了什么?

 static void runTimeConverter()
{   
 double arrivalTime;
 arrivalTime = getArrivalTime();
 string time = DateTime.ParseExact(arrivalTime).ToString("HH:mm tt");
 Console.WriteLine("Equals " + time);
}

 static double getArrivalTime()

    {
        Console.WriteLine
            ("Enter time in 24 hour format to convert to 12hour");
        string timeSelected = Console.ReadLine();
        int timeInput = int.Parse(timeSelected);
        return timeInput;
    }
Run Code Online (Sandbox Code Playgroud)

c# datetime

-3
推荐指数
1
解决办法
149
查看次数