appHandler接口找不到

来源:9-4 测试http服务器(上)

莫言于方

2019-10-26

老师你好,
我在测试那一节学习过程中,在errwrapper_test.go文件中,写测试接口时,发线找不到web.go文件里的appHandler接口。
我直到首字母小写,是私有接口,但是视频里就可以呢?
终端打印这个:

# command-line-arguments [command-line-arguments.test]
./errWrapper_test.go:16:5: undefined: appHandler
./errWrapper_test.go:23:8: undefined: errWrapper

Compilation finished with exit code 2

目录结构:

图片描述
代码:

package main

import (
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"
)

func errPanic(writer http.ResponseWriter,request *http.Request) error {
	panic(123)
}

func TestErrWrapper(t *testing.T) {
	tests := []struct {
		h appHandler
		code int
		message string
	}{
		{errPanic, 500,""},
	}
	for _,tt :=range tests {
		f := errWrapper(tt.h)
		resp := httptest.NewRecorder()
		request := httptest.NewRequest(http.MethodGet,"",nil)
		f(resp,request)
		all,_ := ioutil.ReadAll(resp.Body)
		body := string(all)
		if resp.Code != tt.code || body != tt.message {
			t.Errorf("except (%d,%s);" +
				"got (%d,%s)",tt.code,tt.message,resp.Code,body)
		}
	}
}

写回答

1回答

ccmouse

2019-10-26

也有可能是视频剪辑的问题,不过可以参考一下课程配套代码:https://git.imooc.com/coding-180/coding-180/src/master/lang/errhandling/filelistingserver/web.go#L12

0
0

Google资深工程师深度讲解Go语言 由浅入深掌握Go语言

语法+分布式爬虫实战 为转型工程师量身打造

5995 学习 · 1909 问题

查看课程