1 匿名声明。
tests := []struct {
key, val string
ok bool
}{
{"имя", "v", false}, // key must be ascii
{"name", "валю", true}, // value may be non-ascii
{"", "v", false}, // key must be non-empty
{"k", "", true}, // value may be empty
}
这种模式常用。定义一张常量表,表的格式就这个地方用一下就可以。
2 集体声明:
type (
// Echo is the top-level framework instance.
Echo struct {
server engine.Server
premiddleware []MiddlewareFunc
middleware []MiddlewareFunc
maxParam *int
notFoundHandler HandlerFunc
httpErrorHandler HTTPErrorHandler
binder Binder
renderer Renderer
pool sync.Pool
debug bool
router *Router
logger log.Logger
}
// Route contains a handler and information for matching against requests.
Route struct {
Method string
Path string
Handler string
}
// HTTPError represents an error that occurred while handling a request.
HTTPError struct {
Code int
Message string
}
**************
)
比单个type XXX struct 省事情了。Golang的不啰嗦。
相近的东西是:
import, var, const语句。这样减少了程序中间的噪声信号。
例如,python这种,Java都这样。每次都写一个import。
import email.parser
import email.message
import http
import io
import os
import re
import socket
import collections
from urllib.parse import urlsplit
这个看似小事情,实际上很有用。
有疑问加站长微信联系(非本文作者)