如何阅读Go文档?

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

How to read the Go docs?

问题

我创建了一个简单的Go程序(基本上只是示例代码):

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func getPage(url string) (body []byte, err error) {
	resp, err := http.Get(url)

	body = nil

	if (err != nil) {
		return
	}

	defer resp.Body.Close()
	body, err = ioutil.ReadAll(resp.Body)

	return
}

func main() {
	startUrl := "http://slashdot.org/"

	body, err := getPage(startUrl)

	if (err != nil) {
		fmt.Println("Error: " , err)
	}

	fmt.Println(body)
}

我正在尝试阅读文档以了解它们如何配合使用。

第一个问题:http.Get()。它在文档中找不到(在http://golang.org/pkg/net/http/)。但实际上它是存在的,只是在Response下面。然而,还有其他两个Get()函数。我怎么知道net/http.Get实际上是Response类型的Get()?

无论如何,http.Get()返回一个带有Bodyio.ReadCloser的Response。ioutil.ReadAll()接受一个io.Reader - 但是我如何找到其他接受它作为参数的函数?文档似乎有点“反向” - 如果我知道我想要哪个函数,我可以找到文档,但如果我有一个类型,我如何找到与之兼容的函数?

英文:

I've created a simple go program (basically just example code):

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

func getPage(url string) (body []byte, err error) {
	resp, err := http.Get(url)

	body = nil

	if (err != nil) {
		return
	}

	defer resp.Body.Close()
	body, err = ioutil.ReadAll(resp.Body)

	return
}

func main() {
	startUrl := "http://slashdot.org/"

	body, err := getPage(startUrl)

	if (err != nil) {
		fmt.Println("Error: " , err)
	}

	fmt.Println(body)
}

I'm trying to go through the docs to understand how it all fits together.

First issue: http.Get(). It's not in the docs (at http://golang.org/pkg/net/http/). Except it is, but under Response. However there are 2 other Get() functions. How do I know that net/http.Get is actually the Get() on the Response type?

Anyway, so http.Get() returns a Response with a Body of io.ReadCloser. ioutil.ReadAll() takes an io.Reader - but how can I find other functions that accept this as a parameter? It kind of seems like the docs are 'backwards' - if I know which function I want I can find docs, but if I have a type, how can I find what functions will work with it?

答案1

得分: 7

func (c *Client) Get(url string) (resp *Response, err error)
func (h Header) Get(key string) string
func Get(url string) (resp *Response, err error)

请参阅函数声明的方式

func和函数名(Get)之间,有接收器类型和名称(在括号之间)。只有其中一个函数没有接收器,并且必须直接在包名(http)前面调用。这就是你需要的那个函数。

英文:

The functions are defined like this :

func (c *Client) Get(url string) (resp *Response, err error)
func (h Header) Get(key string) string
func Get(url string) (resp *Response, err error)

See how functions are declared.

Between func and the name of the function (Get), you have the receiver type and name (between parenthesis). Only one of those functions has no receiver and must be called directly prefixed by the package name (http). That's the one you need.

答案2

得分: 0

关于你的io.Readerio.ReadCloser问题:

这些都是接口,如果你对它们不熟悉,请在这里阅读相关内容(http://golang.org/doc/effective_go.html#interfaces_and_types)

接口基本上是一组方法,这两个接口的定义如下:

type Reader interface{
     Read([]byte)(int, error)
}

type ReadCloser interface{
     Read([]byte)(int, error)
     Close()
}

这意味着任何具有上述签名的具体数据类型都可以作为io.Reader传递。满足io.ReadCloser的数据类型肯定会这样做,因为它必须提供Read方法和额外的Close方法。

所以你可以简单地将你的ReadCloser传递给Reader

Go中接口的工作方式一开始可能有点难以理解,因为它们是如此隐式,但它们非常强大,给你很多可能性。一定要阅读我上面链接的文本。我在开始使用Go时读了整个文档,它让事情变得更容易。

英文:

Concerning your io.Reader and io.ReadCloser question:

Those are interfaces, if you are not familiar with them read up on them here

Interfaces are basically sets of methods, these two are defined as follows:

type Reader interface{
     Read([]byte)(int, error)
}

type ReadCloser interface{
     Read([]byte)(int, error)
     Close()
}

This means any concrete datatype that has a Read Method with the above signature can be passed on as a io.Reader. A datatype that satisfies io.ReadCloser definitely does this, since it has to provide the Read method and an additional close Method.

So you can simply pass your ReadCloser on as Reader.

The way interfaces work in go is a little hard to grasp at the beginning since they are so implicit,
however they are very powerful and give you lot's of possibilities. Definitely read the text I linked above. I read the whole thing down, when I was starting with go and it made things a lot easier.

huangapple
  • 本文由 发表于 2013年3月15日 21:43:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/15434022.html
匿名

发表评论

匿名网友

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

确定