Go构造函数类型 [英] Go constructor type

查看:195
本文介绍了Go构造函数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //客户端可用于向客户端发送请求Google地图WebService API 
类型客户端结构{
httpClient * http.Client
apiKey字符串
baseURL字符串
clientID字符串
签名[]字节
requestsPerSecond int
rateLimiter chan int
}

// ClientOption是NewClient(...)的构造函数选项的类型。
类型ClientOption func(* Client)错误

var defaultRequestsPerSecond = 10

// NewClient构造一个新的客户端,它可以向Google Maps WebService API发出请求。
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。它是一个指向客户端的函数类型,它返回一个错误?然后在NewClient中,它看起来像需要一个ClientOption类型的数组并返回一个指向Client的新指针。我不确定这是否正确,如果任何人都可以更多地解释,或者给我一个类似于很多Javascript库的类比

  options:{
property1:someproperty,
property2:another property
}

在构造函数中。

解决方案

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



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

<$ p $客户端选项{
返回func(c *客户端)错误{
c.timeout =持续时间
返回零
}
}

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

它可以按如下方式使用:

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

请参阅自我参考功能 Rob Pike和Dave Cheney的选项文章


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.

解决方案

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.

这篇关于Go构造函数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆