导入但未定义?去执行。

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

imported but undefined? Go

问题

我想使用 "http" 包,并尝试导入它。

package main

import (
	"http"
)

func main() {
	resp, err := http.Get("https://api.github.com/repos/otiai10/myFirstGo")
	if err != nil {
		// do something
	}
	if resp != nil {
		// do something
	}
}

但是我得到了以下输出:

% go run httpget.go
# command-line-arguments
./httpget.go:4: imported and not used: "http"
./httpget.go:8: undefined: http

我看到了这个问题:https://stackoverflow.com/questions/15405973/strange-golang-package-import-issue

这是相同的问题吗?还是我使用了错误的 "import" 或 "http" 的方式?

英文:

I want to use "http" package, and try to import

package main

import (
	"http"
)

func main() {
    resp, err := http.Get("https://api.github.com/repos/otiai10/myFirstGo")
    if err != nil {
	    // do something
    }
    if resp != nil {
    	// do something
    }
}

and got outputs below

% go run httpget.go
# command-line-arguments
./httpget.go:4: imported and not used: "http"
./httpget.go:8: undefined: http

I saw this question : https://stackoverflow.com/questions/15405973/strange-golang-package-import-issue

Is this the same problem? or did I use 'import' or 'http' in wrong way?

答案1

得分: 16

你想要导入的包名叫做"net/http",而不是"http"。请尝试以下代码:

import (
    "net/http"
)
英文:

The package you want to import is called "net/http", not "http". Try:

import (
    "net/http"
)

huangapple
  • 本文由 发表于 2013年8月16日 12:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/18265786.html
匿名

发表评论

匿名网友

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

确定