一,序
2020年8月3我来实习了,试想一个Java都还没学会熟练使用的人,却要用golang去重新做项目,我能退缩吗,当然不能,于是我用一周的时间来熟悉golang,
二,基础学习
于是我对基础的数据类型,指针,运算符,程序流程控制,函数,包,错误处理,数组,切片,map,结构体,方法,封装继承接口
以及多态,文件操作,以及JSON序列化反序列化,单元测试,进程,线程,反射,管道,socket编程,redis,等有了初步了解,是不是现在就成功了,肿么可能,我要立马上线,去拿项目读源码学习,于是开始了我的配置环境阶段
三,环境安装
我安装的是最新的go版本1.14,下载的是go1.14.windows-amd64.msi 安装文件。文件需要到https://golang.org/ 去下载。由于众所周知的原因,需要翻墙,否则网站打不开。
安装很顺利。安装完后打开一个cmd窗口,输入:
E:\goproject>go version
go version go1.14.6 windows/amd64
说明已经安装成功。
四,项目操练
想着到github上找一个go语言编写的项目弄到本地查看编译下。找到一个bee,beego在如何获取地方看到:
go get github.com/astaxie/beego
这是要在cmd 窗口直接输入这行命令就能获取。我于是试了下。可是,输入命令后看着是在运行,但是运行结束后,报以下提示信息,
原来是代理的问题,
unrecognized import path "golang.org/x/text/transform": https fetch: Get "https://golang.org/x/text/transform?go-get=1":
dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly respond
after a period of time, or established connection failed because connected host has failed to respond.
unrecognized import path "golang.org/x/text/unicode/norm": https fetch: Get "https://golang.org/x/text/unicode/norm?go-g
et=1": dial tcp 216.239.37.1:443: connectex: A connection attempt failed because the connected party did not properly re
spond after a period of time, or established connection failed because connected host has failed to respond.
# cd .; git clone -- https://github.com/gadelkareem/delve C:\Users\luckly\go\src\github.com\gadelkareem\delve
Cloning into 'C:\Users\luckly\go\src\github.com\gadelkareem\delve'...
error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 104
fatal: the remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
package github.com/gadelkareem/delve/pkg/terminal: exit status 128
package github.com/gadelkareem/delve/service: cannot find package "github.com/gadelkareem/delve/service" in any of:
F:\sofaware\GO\src\github.com\gadelkareem\delve\service (from $GOROOT)
C:\Users\luckly\go\src\github.com\gadelkareem\delve\service (from $GOPATH)
package github.com/gadelkareem/delve/service/debugger: cannot find package "github.com/gadelkareem/delve/service/debugger" in any of:
F:\sofaware\GO\src\github.com\gadelkareem\delve\service\debugger (from $GOROOT)
C:\Users\luckly\go\src\github.com\gadelkareem\delve\service\debugger (from $GOPATH)
package github.com/gadelkareem/delve/service/rpc2: cannot find package "github.com/gadelkareem/delve/service/rpc2" in any of:
F:\sofaware\GO\src\github.com\gadelkareem\delve\service\rpc2 (from $GOROOT)
C:\Users\luckly\go\src\github.com\gadelkareem\delve\service\rpc2 (from $GOPATH)
package github.com/gadelkareem/delve/service/rpccommon: cannot find package "github.com/gadelkareem/delve/service/rpccommon" in any of:
F:\sofaware\GO\src\github.com\gadelkareem\delve\service\rpccommon (from $GOROOT)
C:\Users\luckly\go\src\github.com\gadelkareem\delve\service\rpccommon (from $GOPATH)
# cd C:\Users\luckly\go\src\github.com\astaxie\beego; git pull --ff-only
fatal: not a git repository (or any parent up to mount point /cygdrive)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
package github.com/astaxie/beego/swagger: exit status 128
package github.com/astaxie/beego/utils: cannot find package "github.com/astaxie/beego/utils" in any of:
F:\sofaware\GO\src\github.com\astaxie\beego\utils (from $GOROOT)
C:\Users\luckly\go\src\github.com\astaxie\beego\utils (from $GOPATH)
于是我发挥了爱学习,爱搜索,爱询问的能力,了解到了有代理这回事,使用go.mod来进行包管理
GO111MODULE
GO111MODULE
有三个值:off
, on
和auto(默认值)
。
-
GO111MODULE=off
,go命令行将不会支持module功能,寻找依赖包的方式将会沿用旧版本那种通过vendor目录或者GOPATH模式来查找。 -
GO111MODULE=on
,go命令行会使用modules,而一点也不会去GOPATH目录下查找。 -
GO111MODULE=auto
,默认值,go命令行将会根据当前目录来决定是否启用module功能。这种情况下可以分为两种情形:
- 当前目录在GOPATH/src之外且该目录包含go.mod文件
- 当前文件在包含go.mod文件的目录下面。
那么我们就来设置吧,我打开命令行:
C:\Users\luckly>go env -w GO111MODULE=on
C:\Users\luckly>go env -w GOPROXY=https://goproxy.io,direct
并进行了简单测试,新建一个名为 main.go的项目,项目路径 e:\goproject (注意,该路径并不在GOPATH里)
当我再次打开命令行的时候
C:\Users\luckly>go get github.com/beego/bee
go: downloading github.com/beego/bee v1.12.0
go: github.com/beego/bee upgrade => v1.12.0
go: downloading github.com/gorilla/websocket v1.4.2
go: downloading github.com/astaxie/beego v1.12.1
go: downloading github.com/lib/pq v1.7.0
go: downloading github.com/go-sql-driver/mysql v1.5.0
go: downloading github.com/gadelkareem/delve v1.4.2-0.20200619175259-dcd0133
go: downloading github.com/smartwalle/pongo2render v1.0.1
go: downloading gopkg.in/yaml.v2 v2.3.0
go: downloading github.com/pelletier/go-toml v1.2.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/fsnotify/fsnotify v1.4.9
go: downloading github.com/flosch/pongo2 v0.0.0-20200529170236-5abacdfa4915
go: downloading golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9
go: downloading github.com/spf13/viper v1.7.0
go: downloading github.com/hashicorp/hcl v1.0.0
go: downloading github.com/spf13/pflag v1.0.3
go: downloading github.com/mitchellh/mapstructure v1.1.2
go: downloading github.com/spf13/jwalterweatherman v1.0.0
go: downloading gopkg.in/ini.v1 v1.51.0
go: downloading github.com/spf13/cast v1.3.0
go: downloading github.com/magiconair/properties v1.8.1
go: downloading github.com/subosito/gotenv v1.2.0
go: downloading github.com/spf13/afero v1.1.2
go: downloading golang.org/x/text v0.3.2
go: downloading github.com/sirupsen/logrus v1.6.0
go: downloading github.com/mattn/go-colorable v0.0.9
go: downloading github.com/cosiner/argv v0.1.0
go: downloading github.com/mattn/go-isatty v0.0.3
go: downloading golang.org/x/arch v0.0.0-20190927153633-4e8777c89be4
go: downloading github.com/peterh/liner v0.0.0-20170317030525-88609521dc4b
go: downloading github.com/hashicorp/golang-lru v0.5.4
go: downloading go.starlark.net v0.0.0-20190702223751-32f345186213
go: downloading github.com/konsorten/go-windows-terminal-sequences v1.0.3
竟然成功了,
于是我又紧追其后,在命令行输入了
C:\Users\luckly>bee
Bee is a Fast and Flexible tool for managing your Beego Web Application.
USAGE
bee command [arguments]
AVAILABLE COMMANDS
version Prints the current Bee version
migrate Runs database migrations
api Creates a Beego API application
bale Transforms non-Go files to Go source files
fix Fixes your application by making it compatible with newer versions of Beego
pro Source code generator
dlv Start a debugging session using Delve
dockerize Generates a Dockerfile for your Beego application
generate Source code generator
hprose Creates an RPC application based on Hprose and Beego frameworks
new Creates a Beego application
pack Compresses a Beego application into a single file
rs Run customized scripts
run Run the application by starting a local development server
server serving static content over HTTP on port
Use bee help [command] for more information about a command.
ADDITIONAL HELP TOPICS
Use bee help [topic] for more information about that topic.
又成功了,至次就成功了吗?没有,为啥?
bee下载到那儿了,不知道,罪过罪过,在我的go语言安装目录的,src ,pkg等下面都没找到,这难道没安上?还是哪里有问题。各种查找。最后终于找到一个解决办法。需要自己创建一个工作区目录。为什么不使用安装目录,是因为要保证安装目录的纯洁性。
打开一看,咋跑C盘去了,仔细一看是自己把代码敲错了,自动添加到C盘了,
我在F盘创建了一个工作目录:F:\software\GO,目录必须是英文,不能有汉字。在这个目录中还要创建3个目录。分别是src,pkg,bin三个目录。并且要在windows系统变量中增加GOPATH,值设置为:F:\software\GO。终于改过来了
然后再打开一个cmd窗口,输入:
go get github.com/beego/bee
执行成功后,可以看到在工作区的src目录下已经有了相应的项目文件。在github.com目录下。
五,此外附上我用过的一些资料
可以考虑 http://tour.studygolang.com 以及 http://books.studygolang.com/gobyexample/
https://www.json.cn/#
https://www.oschina.net/search?scope=project&q=golang
1.开源项目总结
开源项目
以上是我上周的进展,下周继续努力
i)
以上是我上周的进展,下周继续努力
转载:https://blog.csdn.net/qq_39132095/article/details/107911069