小编sys*_*cll的帖子

Golang返回指向接口的指针抛出错误

我在Go中有一个基本功能,它打开一个文件并尝试解码其JSON内容.

我试图提取默认json.NewDecoder()函数,以便我可以在我的测试中轻松模拟它.

但是,我的实现似乎返回一个错误:

不能使用json.NewDecoder(类型func(io.Reader)*json.Decoder)作为NewConfig参数中的类型decoderFactory

码:

package main

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

type openFile func(name string) (*os.File, error)

type decoderFactory func(r io.Reader) decoder

type decoder interface {
    Decode(v interface{}) error
}

type Config struct {
    ConsumerKey,
    ConsumerSecret,
    AccessToken,
    AccessTokenSecret string
}

func NewConfig(open openFile, d decoderFactory) (*Config, error) {
    c := new(Config)
    file, err := open("some.file")
    if err != nil {
        return nil, fmt.Errorf("error opening config file")
    }
    defer file.Close()

    decoder := d(file)
    if err := …
Run Code Online (Sandbox Code Playgroud)

testing pointers interface mocking go

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

在单元测试中模拟 context.Done()

我有一个 HTTP 处理程序,它为每个请求设置上下文截止日期:

func submitHandler(stream chan data) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
        defer cancel()

        // read request body, etc.

        select {
        case stream <- req:
            w.WriteHeader(http.StatusNoContent)
        case <-ctx.Done():
            err := ctx.Err()
            if err == context.DeadlineExceeded {
                w.WriteHeader(http.StatusRequestTimeout)
            }
            log.Printf("context done: %v", err)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我很容易测试http.StatusNoContent标题,但我不确定如何<-ctx.Done()在 select 语句中测试案例。

在我的测试用例中,我构建了一个模拟context.Context并将其传递给req.WithContext()我的模拟上的方法http.Request,但是,返回的状态代码总是http.StatusNoContent使我相信该select语句始终属于我的测试中的第一个案例。

type mockContext struct{}

func (ctx mockContext) Deadline() (deadline time.Time, …
Run Code Online (Sandbox Code Playgroud)

unit-testing go

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

在 Express.js 应用程序中提供客户端 JavaScript 服务

我最近开始使用 Express 构建我的第一个 Node.js 应用程序。我已经使用最新的 Express 生成器创建了一个骨架应用程序,并且我已经使用 Jade 和 CSS 成功创建了几种不同的布局 - 这一切都运行良好。

但是,我似乎无法让我的客户端 JS 工作。在我的公共文件夹中,我有一个单独的前端 JS 文件,它包含的唯一内容是一个警报(仅用于测试目的)。我可以成功导航到浏览器中的文件,并且没有收到任何控制台错误,但警报从未触发 - 我做错了什么?

javascript node.js express

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

Golang函数包含匿名作用域

谁能给我一个解释,何时以及为什么在函数中使用匿名作用域?(我不确定它的实际名称)。

我得到了一些旧代码来维护,某些功能包含了我从未见过的“作用域”:

(为演示目的而简化)

func DoSomething(someBoolValue bool) string {
    if someBoolValue {
        // do some stuff
        return "yes"
    }
    {
        // weird scope code
    }
    return "no"
}
Run Code Online (Sandbox Code Playgroud)

我创建了一个Go Playground来演示一些实际代码(抛出错误)。

scope go

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

使用 HTTP 请求上下文管理通道

我有一个基本的 HTTP 服务器,它接受请求并从数据存储返回数据。

每个 HTTP 请求都会执行以下操作:

  1. 创建一个带有超时时间的上下文
  2. 创建读取请求(自定义类型)
  3. 将读取请求推送到通道
  4. 等待响应并提供数据

这是基本的伪代码:

package main

import (
    "context"
    "net/http"
    "time"
)

type dataRequest struct {
    data chan string
    ctx  context.Context
}

func handler(reqStream chan dataRequest) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
        defer cancel()

        req := dataRequest{
            data: make(chan string),
            ctx:  ctx,
        }

        select {
        case reqStream <- req:
            // request pushed to que
        case <-ctx.Done():
            // don't push onto reqStream if ctx done
        }

        select …
Run Code Online (Sandbox Code Playgroud)

concurrency http channel go

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