Gin 是一款用 Go(Golang) 编写的 web 框架
Gin is a web framework written in Go (Golang). It features a martini-like API with much better performance, up to 40 times faster thanks to httprouter
因为 httprouter, 它提供了更高的性能
这里演示一下如何安装 Gin
Tip: 当前的版本为 Gin 1.2 和 Go 1.10 (但是实验环境下,没有使用最新的版本)
[root@h160 ~]# hostnamectl
Static hostname: h160
Icon name: computer-vm
Chassis: vm
Machine ID: d46f9440d4be429ea66b726977adf233
Boot ID: a66d0c83dd3b4934bc9e1bcc34c9be91
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-862.2.3.el7.x86_64
Architecture: x86-64
[root@h160 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:c9:c7:04 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
valid_lft 80184sec preferred_lft 80184sec
inet6 fe80::5054:ff:fec9:c704/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:48:f4:2c brd ff:ff:ff:ff:ff:ff
inet 192.168.56.160/24 brd 192.168.56.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe48:f42c/64 scope link
valid_lft forever preferred_lft forever
[root@h160 ~]# rpm -qa | grep golang
golang-1.9.4-1.el7.x86_64
golang-src-1.9.4-1.el7.noarch
golang-bin-1.9.4-1.el7.x86_64
[root@h160 ~]#
[root@h160 ~]# go version
go version go1.9.4 linux/amd64
[root@h160 ~]#
[vagrant@h160 vagrant]$ grep GOPATH ~/.bashrc
export GOPATH=/vagrant/go
[vagrant@h160 vagrant]$ env | grep -i GOPA
GOPATH=/vagrant/go
[vagrant@h160 vagrant]$
[vagrant@h160 ~]$ cd $GOPATH
[vagrant@h160 go]$ pwd
/vagrant/go
[vagrant@h160 go]$ ls
[vagrant@h160 go]$
[vagrant@h160 go]$ ls -a
. ..
[vagrant@h160 go]$ go get -u github.com/gin-gonic/gin
[vagrant@h160 go]$ ls -a
. .. pkg src
[vagrant@h160 go]$ tree pkg/
pkg/
`-- linux_amd64
|-- github.com
| |-- gin-contrib
| | `-- sse.a
| |-- gin-gonic
| | |-- gin
| | | |-- binding.a
| | | |-- json.a
| | | `-- render.a
| | `-- gin.a
| |-- golang
| | `-- protobuf
| | `-- proto.a
| |-- mattn
| | `-- go-isatty.a
| `-- ugorji
| `-- go
| `-- codec.a
`-- gopkg.in
|-- go-playground
| `-- validator.v8.a
`-- yaml.v2.a
12 directories, 10 files
[vagrant@h160 go]$ tree src/
src/
|-- github.com
| |-- gin-contrib
| | `-- sse
| | |-- LICENSE
| | |-- README.md
| | |-- sse-decoder.go
| | |-- sse-decoder_test.go
| | |-- sse-encoder.go
| | |-- sse_test.go
| | `-- writer.go
| |-- gin-gonic
| | `-- gin
| | |-- auth.go
| | |-- AUTHORS.md
| | |-- auth_test.go
| | |-- BENCHMARKS.md
| | |-- benchmarks_test.go
| | |-- binding
| | | |-- binding_body_test.go
| | | |-- binding.go
| | | |-- binding_test.go
| | | |-- default_validator.go
| | | |-- example
| | | | |-- test.pb.go
| | | | `-- test.proto
| | | |-- form.go
| | | |-- form_mapping.go
| | | |-- json.go
| | | |-- msgpack.go
| | | |-- protobuf.go
| | | |-- query.go
| | | |-- validate_test.go
| | | `-- xml.go
| | |-- CHANGELOG.md
| | |-- codecov.yml
| | |-- CODE_OF_CONDUCT.md
| | |-- context_appengine.go
| | |-- context.go
| | |-- context_test.go
| | |-- CONTRIBUTING.md
| | |-- coverage.sh
| | |-- debug.go
| | |-- debug_test.go
| | |-- deprecated.go
| | |-- deprecated_test.go
| | |-- doc.go
| | |-- errors.go
| | |-- errors_test.go
| | |-- examples
| | | |-- app-engine
| | | | |-- app.yaml
| | | | |-- hello.go
| | | | `-- README.md
| | | |-- assets-in-binary
| | | | |-- assets.go
| | | | |-- html
| | | | | |-- bar.tmpl
| | | | | `-- index.tmpl
| | | | |-- main.go
| | | | `-- README.md
| | | |-- auto-tls
| | | | |-- example1
| | | | | `-- main.go
| | | | `-- example2
| | | | `-- main.go
| | | |-- basic
| | | | |-- main.go
| | | | `-- main_test.go
| | | |-- custom-validation
| | | | `-- server.go
| | | |-- favicon
| | | | |-- favicon.ico
| | | | `-- main.go
| | | |-- graceful-shutdown
| | | | |-- close
| | | | | `-- server.go
| | | | `-- graceful-shutdown
| | | | `-- server.go
| | | |-- grpc
| | | | |-- gin
| | | | | `-- main.go
| | | | |-- grpc
| | | | | `-- server.go
| | | | |-- pb
| | | | | |-- helloworld.pb.go
| | | | | `-- helloworld.proto
| | | | `-- README.md
| | | |-- http2
| | | | |-- main.go
| | | | |-- README.md
| | | | `-- testdata
| | | | |-- ca.pem
| | | | |-- server.key
| | | | `-- server.pem
| | | |-- http-pusher
| | | | |-- assets
| | | | | `-- app.js
| | | | |-- main.go
| | | | `-- testdata
| | | | |-- ca.pem
| | | | |-- server.key
| | | | `-- server.pem
| | | |-- multiple-service
| | | | `-- main.go
| | | |-- realtime-advanced
| | | | |-- main.go
| | | | |-- Makefile
| | | | |-- resources
| | | | | |-- room_login.templ.html
| | | | | `-- static
| | | | | |-- epoch.min.css
| | | | | |-- epoch.min.js
| | | | | |-- prismjs.min.css
| | | | | |-- prismjs.min.js
| | | | | `-- realtime.js
| | | | |-- rooms.go
| | | | |-- routes.go
| | | | `-- stats.go
| | | |-- realtime-chat
| | | | |-- main.go
| | | | |-- Makefile
| | | | |-- rooms.go
| | | | `-- template.go
| | | |-- struct-lvl-validations
| | | | |-- README.md
| | | | `-- server.go
| | | |-- template
| | | | `-- main.go
| | | `-- upload-file
| | | |-- multiple
| | | | |-- main.go
| | | | `-- public
| | | | `-- index.html
| | | `-- single
| | | |-- main.go
| | | `-- public
| | | `-- index.html
| | |-- fixtures
| | | |-- basic
| | | | |-- hello.tmpl
| | | | `-- raw.tmpl
| | | `-- testdata
| | | |-- cert.pem
| | | `-- key.pem
| | |-- fs.go
| | |-- gin.go
| | |-- gin_integration_test.go
| | |-- ginS
| | | |-- gins.go
| | | `-- README.md
| | |-- gin_test.go
| | |-- githubapi_test.go
| | |-- json
| | | |-- json.go
| | | `-- jsoniter.go
| | |-- LICENSE
| | |-- logger.go
| | |-- logger_test.go
| | |-- Makefile
| | |-- middleware_test.go
| | |-- mode.go
| | |-- mode_test.go
| | |-- path.go
| | |-- path_test.go
| | |-- README.md
| | |-- recovery.go
| | |-- recovery_test.go
| | |-- render
| | | |-- data.go
| | | |-- html.go
| | | |-- json.go
| | | |-- msgpack.go
| | | |-- reader.go
| | | |-- redirect.go
| | | |-- render.go
| | | |-- render_test.go
| | | |-- text.go
| | | |-- xml.go
| | | `-- yaml.go
| | |-- response_writer_1.7.go
| | |-- response_writer_1.8.go
| | |-- response_writer.go
| | |-- response_writer_test.go
| | |-- routergroup.go
| | |-- routergroup_test.go
| | |-- routes_test.go
| | |-- test_helpers.go
| | |-- tree.go
| | |-- tree_test.go
| | |-- utils.go
| | |-- utils_test.go
| | |-- vendor
| | | `-- vendor.json
| | `-- wercker.yml
| |-- golang
| | `-- protobuf
| | |-- AUTHORS
| | |-- conformance
| | | |-- conformance.go
| | | |-- conformance.sh
| | | |-- failure_list_go.txt
| | | |-- internal
| | | | `-- conformance_proto
| | | | |-- conformance.pb.go
| | | | `-- conformance.proto
| | | |-- Makefile
| | | `-- test.sh
| | |-- CONTRIBUTORS
| | |-- descriptor
| | | |-- descriptor.go
| | | `-- descriptor_test.go
| | |-- jsonpb
| | | |-- jsonpb.go
| | | |-- jsonpb_test.go
| | | `-- jsonpb_test_proto
| | | |-- more_test_objects.pb.go
| | | |-- more_test_objects.proto
| | | |-- test_objects.pb.go
| | | `-- test_objects.proto
| | |-- LICENSE
| | |-- Makefile
| | |-- proto
| | | |-- all_test.go
| | | |-- any_test.go
| | | |-- clone.go
| | | |-- clone_test.go
| | | |-- decode.go
| | | |-- decode_test.go
| | | |-- discard.go
| | | |-- discard_test.go
| | | |-- encode.go
| | | |-- encode_test.go
| | | |-- equal.go
| | | |-- equal_test.go
| | | |-- extensions.go
| | | |-- extensions_test.go
| | | |-- lib.go
| | | |-- map_test.go
| | | |-- message_set.go
| | | |-- message_set_test.go
| | | |-- pointer_reflect.go
| | | |-- pointer_unsafe.go
| | | |-- properties.go
| | | |-- proto3_proto
| | | | |-- proto3.pb.go
| | | | `-- proto3.proto
| | | |-- proto3_test.go
| | | |-- size2_test.go
| | | |-- size_test.go
| | | |-- table_marshal.go
| | | |-- table_merge.go
| | | |-- table_unmarshal.go
| | | |-- test_proto
| | | | |-- test.pb.go
| | | | `-- test.proto
| | | |-- text.go
| | | |-- text_parser.go
| | | |-- text_parser_test.go
| | | `-- text_test.go
| | |-- protoc-gen-go
| | | |-- descriptor
| | | | |-- descriptor.pb.go
| | | | `-- descriptor.proto
| | | |-- doc.go
| | | |-- generator
| | | | |-- generator.go
| | | | |-- internal
| | | | | `-- remap
| | | | | |-- remap.go
| | | | | `-- remap_test.go
| | | | `-- name_test.go
| | | |-- golden_test.go
| | | |-- grpc
| | | | `-- grpc.go
| | | |-- link_grpc.go
| | | |-- main.go
| | | |-- plugin
| | | | |-- plugin.pb.go
| | | | |-- plugin.pb.golden
| | | | `-- plugin.proto
| | | `-- testdata
| | | |-- deprecated
| | | | |-- deprecated.pb.go
| | | | `-- deprecated.proto
| | | |-- extension_base
| | | | |-- extension_base.pb.go
| | | | `-- extension_base.proto
| | | |-- extension_extra
| | | | |-- extension_extra.pb.go
| | | | `-- extension_extra.proto
| | | |-- extension_test.go
| | | |-- extension_user
| | | | |-- extension_user.pb.go
| | | | `-- extension_user.proto
| | | |-- grpc
| | | | |-- grpc.pb.go
| | | | `-- grpc.proto
| | | |-- import_public
| | | | |-- a.pb.go
| | | | |-- a.proto
| | | | |-- b.pb.go
| | | | |-- b.proto
| | | | `-- sub
| | | | |-- a.pb.go
| | | | |-- a.proto
| | | | |-- b.pb.go
| | | | `-- b.proto
| | | |-- import_public_test.go
| | | |-- imports
| | | | |-- fmt
| | | | | |-- m.pb.go
| | | | | `-- m.proto
| | | | |-- test_a_1
| | | | | |-- m1.pb.go
| | | | | |-- m1.proto
| | | | | |-- m2.pb.go
| | | | | `-- m2.proto
| | | | |-- test_a_2
| | | | | |-- m3.pb.go
| | | | | |-- m3.proto
| | | | | |-- m4.pb.go
| | | | | `-- m4.proto
| | | | |-- test_b_1
| | | | | |-- m1.pb.go
| | | | | |-- m1.proto
| | | | | |-- m2.pb.go
| | | | | `-- m2.proto
| | | | |-- test_import_a1m1.pb.go
| | | | |-- test_import_a1m1.proto
| | | | |-- test_import_a1m2.pb.go
| | | | |-- test_import_a1m2.proto
| | | | |-- test_import_all.pb.go
| | | | `-- test_import_all.proto
| | | |-- main_test.go
| | | |-- multi
| | | | |-- multi1.pb.go
| | | | |-- multi1.proto
| | | | |-- multi2.pb.go
| | | | |-- multi2.proto
| | | | |-- multi3.pb.go
| | | | `-- multi3.proto
| | | |-- my_test
| | | | |-- test.pb.go
| | | | `-- test.proto
| | | `-- proto3
| | | |-- proto3.pb.go
| | | `-- proto3.proto
| | |-- ptypes
| | | |-- any
| | | | |-- any.pb.go
| | | | `-- any.proto
| | | |-- any.go
| | | |-- any_test.go
| | | |-- doc.go
| | | |-- duration
| | | | |-- duration.pb.go
| | | | `-- duration.proto
| | | |-- duration.go
| | | |-- duration_test.go
| | | |-- empty
| | | | |-- empty.pb.go
| | | | `-- empty.proto
| | | |-- struct
| | | | |-- struct.pb.go
| | | | `-- struct.proto
| | | |-- timestamp
| | | | |-- timestamp.pb.go
| | | | `-- timestamp.proto
| | | |-- timestamp.go
| | | |-- timestamp_test.go
| | | `-- wrappers
| | | |-- wrappers.pb.go
| | | `-- wrappers.proto
| | |-- README.md
| | `-- regenerate.sh
| |-- mattn
| | `-- go-isatty
| | |-- doc.go
| | |-- example_test.go
| | |-- isatty_appengine.go
| | |-- isatty_bsd.go
| | |-- isatty_linux.go
| | |-- isatty_linux_ppc64x.go
| | |-- isatty_others.go
| | |-- isatty_others_test.go
| | |-- isatty_solaris.go
| | |-- isatty_windows.go
| | |-- isatty_windows_test.go
| | |-- LICENSE
| | `-- README.md
| `-- ugorji
| `-- go
| |-- codec
| | |-- 0doc.go
| | |-- binc.go
| | |-- build.sh
| | |-- cbor.go
| | |-- cbor_test.go
| | |-- codecgen
| | | |-- gen.go
| | | |-- README.md
| | | `-- z.go
| | |-- codec_test.go
| | |-- decode.go
| | |-- encode.go
| | |-- fast-path.generated.go
| | |-- fast-path.go.tmpl
| | |-- fast-path.not.go
| | |-- gen-dec-array.go.tmpl
| | |-- gen-dec-map.go.tmpl
| | |-- gen-enc-chan.go.tmpl
| | |-- gen.generated.go
| | |-- gen.go
| | |-- gen-helper.generated.go
| | |-- gen-helper.go.tmpl
| | |-- goversion_arrayof_gte_go15.go
| | |-- goversion_arrayof_lt_go15.go
| | |-- goversion_makemap_gte_go19.go
| | |-- goversion_makemap_lt_go19.go
| | |-- goversion_unexportedembeddedptr_gte_go110.go
| | |-- goversion_unexportedembeddedptr_lt_go110.go
| | |-- goversion_unsupported_lt_go14.go
| | |-- goversion_vendor_eq_go15.go
| | |-- goversion_vendor_eq_go16.go
| | |-- goversion_vendor_gte_go17.go
| | |-- goversion_vendor_lt_go15.go
| | |-- helper.go
| | |-- helper_internal.go
| | |-- helper_not_unsafe.go
| | |-- helper_test.go
| | |-- helper_unsafe.go
| | |-- json.go
| | |-- mammoth2_codecgen_generated_test.go
| | |-- mammoth2_generated_test.go
| | |-- mammoth2-test.go.tmpl
| | |-- mammoth_generated_test.go
| | |-- mammoth-test.go.tmpl
| | |-- msgpack.go
| | |-- py_test.go
| | |-- README.md
| | |-- rpc.go
| | |-- shared_test.go
| | |-- simple.go
| | |-- test-cbor-goldens.json
| | |-- test.py
| | |-- values_flex_test.go
| | |-- values_test.go
| | |-- xml.go
| | `-- z_all_test.go
| |-- LICENSE
| |-- msgpack.org.md
| `-- README.md
`-- gopkg.in
|-- go-playground
| `-- validator.v8
| |-- baked_in.go
| |-- benchmarks_test.go
| |-- cache.go
| |-- doc.go
| |-- examples
| | |-- custom
| | | `-- custom.go
| | |-- simple
| | | `-- simple.go
| | `-- struct-level
| | `-- struct_level.go
| |-- examples_test.go
| |-- LICENSE
| |-- logo.png
| |-- README.md
| |-- regexes.go
| |-- util.go
| |-- validator.go
| `-- validator_test.go
`-- yaml.v2
|-- apic.go
|-- decode.go
|-- decode_test.go
|-- emitterc.go
|-- encode.go
|-- encode_test.go
|-- example_embedded_test.go
|-- go.mod
|-- LICENSE
|-- LICENSE.libyaml
|-- NOTICE
|-- parserc.go
|-- readerc.go
|-- README.md
|-- resolve.go
|-- scannerc.go
|-- sorter.go
|-- suite_test.go
|-- writerc.go
|-- yaml.go
|-- yamlh.go
`-- yamlprivateh.go
103 directories, 394 files
[vagrant@h160 go]$
可以看到多出来了很多东西
[vagrant@h160 go]$ vim hello.go
[vagrant@h160 go]$ cat hello.go
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
[vagrant@h160 go]$
[vagrant@h160 go]$ go run hello.go
[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
...
...
...
正在运行的端口与进程
[vagrant@h160 go]$ netstat -ant | grep :80
tcp 0 0 10.0.2.15:55078 103.97.125.131:80 TIME_WAIT
tcp 0 0 10.0.2.15:55082 103.97.125.131:80 TIME_WAIT
tcp6 0 0 :::8080 :::* LISTEN
[vagrant@h160 go]$ ps faux | grep go
vagrant 8053 0.0 0.2 219620 9956 pts/0 Sl+ 16:11 0:00 | \_ go run hello.go
vagrant 8076 0.0 0.1 117336 5724 pts/0 Sl+ 16:11 0:00 | \_ /tmp/go-build529315964/command-line-arguments/_obj/exe/hello
vagrant 8192 0.0 0.0 12492 688 pts/1 S+ 16:13 0:00 \_ grep --color=auto go
[vagrant@h160 go]$
[vagrant@h160 go]$ curl localhost:8080
404 page not found[vagrant@h160 go]$
[vagrant@h160 go]$
console 输出为
[GIN] 2018/07/04 - 16:14:53 | 404 | 1.693µs | ::1 | GET /
[vagrant@h160 go]$ curl localhost:8080/ping
{"message":"pong"}[vagrant@h160 go]$
[vagrant@h160 go]$
console 输出为
[GIN] 2018/07/04 - 16:20:58 | 200 | 202.395µs | ::1 | GET /ping
这么简单的一段代码就实现了一个 web 服务
真的是十分简洁方便的小框架
后面再逐步对此 web 进行功能扩展
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。