Go构造函数类型

huangapple go评论65阅读模式
英文:

Go constructor type

问题

我想知道是否有人可以解释一下这个语法给我:

// Client may be used to make requests to the Google Maps WebService APIs
type Client struct {
httpClient *http.Client
apiKey string
baseURL string
clientID string
signature []byte
requestsPerSecond int
rateLimiter chan int
}

// ClientOption is the type of constructor options for NewClient(...).
type ClientOption func(*Client) error

var defaultRequestsPerSecond = 10

// NewClient constructs a new Client which can make requests to the Google Maps WebService APIs.
func NewClient(options ...ClientOption) (*Client, error) {
c := &Client{requestsPerSecond: defaultRequestsPerSecond}
WithHTTPClient(&http.Client{})(c)
for _, option := range options {
err := option(c)
if err != nil {
return nil, err
}
}
.....

我不明白ClientOption是什么意思。它是一个返回错误的指向Client的函数类型吗?然后在NewClient中,它看起来接受一个ClientOption类型的数组,并返回一个指向Client的新指针。我不确定这是否正确,如果有人能更详细地解释一下或给我一个类似于许多JavaScript库在构造函数中使用的选项的类比,例如:

options: {
property1: "someproperty",
property2: "anotherproperty"
}

英文:

I was wondering if someone could explain this syntax to me:

// Client may be used to make requests to the Google Maps WebService APIs
type Client struct {
	httpClient        *http.Client
	apiKey            string
	baseURL           string
	clientID          string
	signature         []byte
	requestsPerSecond int
	rateLimiter       chan int
}

// ClientOption is the type of constructor options for NewClient(...).
type ClientOption func(*Client) error

var defaultRequestsPerSecond = 10

// NewClient constructs a new Client which can make requests to the Google Maps WebService APIs.
func NewClient(options ...ClientOption) (*Client, error) {
	c := &Client{requestsPerSecond: defaultRequestsPerSecond}
	WithHTTPClient(&http.Client{})(c)
	for _, option := range options {
		err := option(c)
		if err != nil {
			return nil, err
		}
	}
	.....

I don't understand what's going on with ClientOption. Is it a function type on pointer to client that returns an error? And then in NewClient, it looks like it takes in an array of ClientOption types and returns a new pointer to a Client. I'm not sure if that's right and if anyone can explain that more or give me an analogy similar to what a lot of Javascript libraries do with

options: {
	property1: "someproperty",
	property2: "anotherproperty"
}

in a constructor.

答案1

得分: 3

ClientOption是一个接受客户端指针并返回错误(可能为nil)的函数。

例如,这是一个创建ClientOption函数的超时函数:

func WithTimeout(duration time.Duration) ClientOption {
    return func(c *Client) error {
        c.timeout = duration
        return nil
    }
}

NewClient函数接受可变数量的ClientOption参数,这些参数在'options'切片中可用。它创建一个新的Client,通过将客户端指针传递给每个ClientOption来配置它,并返回指针。

可以按以下方式使用它:

client := NewClient(WithTimeout(3 * time.Second))

请参阅Rob Pike的自引用函数和Dave Cheney的选项文章

英文:

A ClientOption is a function accepting a client pointer and returning an error (probably nil).

For example, here's a timeout function creating a ClientOption function:

func WithTimeout(duration time.Duration) ClientOption {
	return func(c *Client) error {
		c.timeout = duration
		return nil
	}
}

The NewClient function accepts a variable number of ClientOption parameters, available in the 'options' slice. It creates a new Client, configures it by passing client pointer to each ClientOption, and returns the pointer.

It could be used as follows:

client := NewClient(WithTimeout(3 * time.Second))

See self referential functions by Rob Pike, and Dave Cheney's options article.

huangapple
  • 本文由 发表于 2017年1月13日 08:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/41625363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定