第3部:在Windows上的初次使用Go语言

之前我只是尝试玩了一下Go语言本身,但这次我想试试周边功能。

使用”get”命令安装应用程序。

可以使用 “get” 命令从公共仓库安装应用程序及其依赖包。我们尝试安装最近备受关注的 “Hugo”,被称为 “Jekyll 后续” 的工具。

    Hugo :: A fast and modern static website engine

由于Hugo可以作为二进制文件分发,所以并不需要从源代码编译。但是,我还是想看看它的内部结构,也可以作为练习使用get命令。另外,我目前正在使用Movable Type作为我的主要工具,需要想办法处理一下,因为MTOS的支持今年就会结束。

获得”get”命令的要求。

Go语言中的一个环境变量,用于指定Go程序源代码和编译生成的可执行文件所在的目录。

在使用 get 命令时,需要将获取的源代码或编译后的模块存储到环境变量 GOPATH 中。如果没有设置 GOPATH,则会收到以下错误提示。

C:>go get
package github.com/spf13/hugo/commands: cannot download, $GOPATH not set. For more details see: go help gopath

以下是有关 GOPATH 的详细解释。
只需查看实际经过 get 命令后的 GOPATH 文件夹内的内容,可能会更快。

C:>go help gopath
The Go path is used to resolve import statements.
It is implemented by and documented in the go/build package.

The GOPATH environment variable lists places to look for Go code.
On Unix, the value is a colon-separated string.
On Windows, the value is a semicolon-separated string.
On Plan 9, the value is a list.

GOPATH must be set to get, build and install packages outside the
standard Go tree.

Each directory listed in GOPATH must have a prescribed structure:

The src/ directory holds source code.  The path below 'src'
determines the import path or executable name.

The pkg/ directory holds installed package objects.
As in the Go tree, each target operating system and
architecture pair has its own subdirectory of pkg
(pkg/GOOS_GOARCH).

If DIR is a directory listed in the GOPATH, a package with
source in DIR/src/foo/bar can be imported as "foo/bar" and
has its compiled form installed to "DIR/pkg/GOOS_GOARCH/foo/bar.a".

The bin/ directory holds compiled commands.
Each command is named for its source directory, but only
the final element, not the entire path.  That is, the
command with source in DIR/src/foo/quux is installed into
DIR/bin/quux, not DIR/bin/foo/quux.  The foo/ is stripped
so that you can add DIR/bin to your PATH to get at the
installed commands.  If the GOBIN environment variable is
set, commands are installed to the directory it names instead
of DIR/bin.

Here's an example directory layout:

    GOPATH=/home/user/gocode

    /home/user/gocode/
        src/
            foo/
                bar/               (go code in package bar)
                    x.go
                quux/              (go code in package main)
                    y.go
        bin/
            quux                   (installed command)
        pkg/
            linux_amd64/
                foo/
                    bar.a          (installed package object)

Go searches each directory listed in GOPATH to find source code,
but new packages are always downloaded into the first directory
in the list.

内部命令

get命令在内部调用以下版本管理工具。

    • svn : Subversion

hg : Mercurial

git : Git

bzr : Bazaar

例如,执行”get”命令时

C:>go get
go: missing Mercurial command. See http://golang.org/s/gogetcmd
package github.com/spf13/hugo/commands
        imports bitbucket.org/pkg/inflect: exec: "hg": executable file not found in %PATH%

如果出现类似的错误,这意味着没有安装任何版本管理工具(在这种情况下是 Mercurial)。由于所有这些工具都有适用于 Windows 的版本,因此根据需要安装一个是明智的选择。

顺便提一下,在 Hugo 中需要安装 Git 和 Mercurial。

代理服务器设置

如果从像 GitHub 或 Bitbucket 这样的熟知仓库中获取,则似乎直接执行上述命令,但对于其他网站,先通过 HTTP/HTTPS 进行一次 fetch,然后再决定使用哪个命令来获取。在受到防火墙/代理阻止的环境中,这样做可能会导致错误。

在这种情况下,需要设置代理。设置方法是设置http_proxy和https_proxy环境变量。例如,可以像这样设置。

SET http_proxy=http://username:password@proxy.exsample.com:8080/
SET https_proxy=%http_proxy%

如果代理设置中包含用户名和密码,不要在注册表中设置环境变量(通过控制面板 -> 系统 -> 系统详细设置 -> [环境变量(N)…]按钮打开环境变量设置界面),而是通过命令提示符等手动设置环境变量,这样更安全…但是这样做很麻烦,是吧。

Hugo 的安装

如果上述的准备工作已就绪,那么Hugo安装只需一条命令即可完成。

由于Hugo的存储库位于spf13/hugo下,因此请按照以下方式启动命令。

C:>go get -v github.com/spf13/hugo
github.com/spf13/hugo (download)
github.com/kardianos/osext (download)
github.com/spf13/afero (download)
github.com/spf13/cast (download)
github.com/spf13/jwalterweatherman (download)
github.com/spf13/cobra (download)
github.com/cpuguy83/go-md2man (download)
github.com/russross/blackfriday (download)
github.com/shurcooL/sanitized_anchor_name (download)
github.com/inconshreveable/mousetrap (download)
github.com/spf13/pflag (download)
github.com/spf13/fsync (download)
github.com/PuerkitoBio/purell (download)
github.com/opennota/urlesc (download)
github.com/miekg/mmark (download)
github.com/BurntSushi/toml (download)
Fetching https://gopkg.in/yaml.v2?go-get=1
Parsing meta tags from https://gopkg.in/yaml.v2?go-get=1 (status code 200)
get "gopkg.in/yaml.v2": found meta tag main.metaImport{Prefix:"gopkg.in/yaml.v2", VCS:"git", RepoRoot:"https://gopkg.in/yaml.v2"} at https://gopkg.in/yaml.v2?go-get=1
gopkg.in/yaml.v2 (download)
github.com/spf13/viper (download)
github.com/kr/pretty (download)
github.com/kr/text (download)
github.com/magiconair/properties (download)
github.com/mitchellh/mapstructure (download)
Fetching https://golang.org/x/text/transform?go-get=1
Parsing meta tags from https://golang.org/x/text/transform?go-get=1 (status code 200)
get "golang.org/x/text/transform": found meta tag main.metaImport{Prefix:"golang.org/x/text", VCS:"git", RepoRoot:"https://go.googlesource.com/text"} at https://golang.org/x/text/transform?go-get=1
get "golang.org/x/text/transform": verifying non-authoritative meta tag
Fetching https://golang.org/x/text?go-get=1
Parsing meta tags from https://golang.org/x/text?go-get=1 (status code 200)
golang.org/x/text (download)
Fetching https://golang.org/x/text/unicode/norm?go-get=1
Parsing meta tags from https://golang.org/x/text/unicode/norm?go-get=1 (status code 200)
get "golang.org/x/text/unicode/norm": found meta tag main.metaImport{Prefix:"golang.org/x/text", VCS:"git", RepoRoot:"https://go.googlesource.com/text"} at https://golang.org/x/text/unicode/norm?go-get=1
get "golang.org/x/text/unicode/norm": verifying non-authoritative meta tag
bitbucket.org/pkg/inflect (download)
github.com/dchest/cssmin (download)
github.com/eknkc/amber (download)
github.com/yosssi/ace (download)
github.com/spf13/nitro (download)
github.com/gorilla/websocket (download)
Fetching https://gopkg.in/fsnotify.v1?go-get=1
Parsing meta tags from https://gopkg.in/fsnotify.v1?go-get=1 (status code 200)
get "gopkg.in/fsnotify.v1": found meta tag main.metaImport{Prefix:"gopkg.in/fsnotify.v1", VCS:"git", RepoRoot:"https://gopkg.in/fsnotify.v1"} at https://gopkg.in/fsnotify.v1?go-get=1
gopkg.in/fsnotify.v1 (download)
github.com/shurcooL/sanitized_anchor_name
github.com/inconshreveable/mousetrap
github.com/spf13/hugo/bufferpool
github.com/kr/text
github.com/kardianos/osext
github.com/spf13/afero
github.com/spf13/jwalterweatherman
github.com/spf13/pflag
github.com/russross/blackfriday
github.com/opennota/urlesc
github.com/BurntSushi/toml
gopkg.in/yaml.v2
github.com/kr/pretty
github.com/spf13/cast
# github.com/opennota/urlesc
runtime: failed to create new OS thread (have 5 already; errno=5)
fatal error: runtime.newosproc
github.com/PuerkitoBio/purell
github.com/spf13/fsync
github.com/spf13/hugo/hugofs
github.com/magiconair/properties
github.com/mitchellh/mapstructure
github.com/cpuguy83/go-md2man/md2man
golang.org/x/text/transform
bitbucket.org/pkg/inflect
github.com/miekg/mmark
github.com/dchest/cssmin
github.com/spf13/cobra
github.com/eknkc/amber/parser
golang.org/x/text/unicode/norm
github.com/yosssi/ace
github.com/spf13/nitro
github.com/spf13/hugo/parser
github.com/spf13/viper
github.com/eknkc/amber
github.com/gorilla/websocket
github.com/spf13/hugo/utils
gopkg.in/fsnotify.v1
github.com/spf13/hugo/transform
github.com/spf13/hugo/watcher
github.com/spf13/hugo/helpers
github.com/spf13/hugo/livereload
github.com/spf13/hugo/source
github.com/spf13/hugo/target
github.com/spf13/hugo/tpl
github.com/spf13/hugo/hugolib
github.com/spf13/hugo/create
github.com/spf13/hugo/commands
github.com/spf13/hugo

C:>hugo version
Hugo Static Site Generator v0.15-DEV BuildDate: 2015-08-24T10:24:01+09:00

如果添加了-v选项,它会显示所有的依赖关系,这样就更容易理解了。

当需要更新时,请使用-u选项。

C:>go get -u -v github.com/spf13/hugo

可以。

对于可执行模块,会被输出到%GOPATH%\bin文件夹中。您可以直接将该文件夹加入系统路径,或者复制到其他位置。

现在

正好是黄金周,让我们去玩吧!

书签

    • Jekyllが許されるのは小学生までだよね – MOL

 

    • CSS – HUGOを使ってサイトを立ち上げる方法 – Qiita

 

    静的サイトジェネレーターHugoでgithubページ – Qiita
广告
将在 10 秒后关闭
bannerAds