Golang依赖管理工具:glide从入门到精通使用

小编 2026-07-09 阅读:1213 评论:0
 这是一个创建于 2017-07-22 05:33:09 的文章,其中的信息可能已经有所发展或是...

 

这是一个创建于 2017-07-22 05:33:09 的文章,其中的信息可能已经有所发展或是发生改变。

介绍

不论是开发Java还是你正在学习的Golang,都会遇到依赖管理问题。Java有牛逼轰轰的Maven和Gradle。 Golang亦有godep、govendor、glide、gvt、gopack等等,本文主要给大家介绍gilde。 glide是Golang的包管理工具,是为了解决Golang依赖问题的。 为什么需要glide? 原因很简单,Go 语言原生包管理的缺陷。罗列一下golang的 get 子命令管理依赖有很多大缺陷:

  • 能拉取源码的平台很有限,绝大多数依赖的是 github.com
  • 不能区分版本,以至于令开发者以最后一项包名作为版本划分
  • 依赖 列表/关系 无法持久化到本地,需要找出所有依赖包然后一个个 go get
  • 只能依赖本地全局仓库(GOPATH/GOROOT),无法将库放置于局部仓库($PROJECT_HOME/vendor)

安装

Golang环境设置

采用vendor目录特性,Go 1.5 做为试验特性加入(需要指定 GO15VENDOREXPERIMENT=1 环境变量),并在 Go 1.6 正式引入的一个概念。多数 go 依赖解决方案都基于它的。GO15VENDOREXPERIMENT 是 Go 1.5 版本新增的一个环境变量,如果将值改为 1 则表示启用。它可以将项目根目录名为 vendor 的目录添加到 Go 的库搜寻路径中,实现一个局部依赖的效果。
特性在 1.5 版本作为实验特性被添加,1.6 中默认被启用,1.7 移除变量加入标准中。
Go 提供了原始的 go get ,让第三方包管理可以基于 go get 做扩展。GO15VENDOREXPERIMENT 特性让局部依赖成为现实。Go 官方在给第三方包管理营造条件以及引导开发者用户至所推荐的方向,促进社区的繁荣。证明了一个语言技术的生态不仅仅只能靠官方或者取决于官方的完善程度。

//设置环境变量 使用vendor目录GO15VENDOREXPERIMENT=1

为什么要选择glide? Glide 是众多实现 GO15VENDOREXPERIMENT 特性的包管理工具之一,但它是本文最为推荐的,具体为什么推荐它,原因很简单,因为它目前最受关注。 几大主要功能:

  • 持久化依赖列表至配置文件中,包括依赖版本(支持范围限定)以及私人仓库等
  • 持久化关系树至 lock 文件中(类似于 yarn 和 cargo),以重复拉取相同版本依赖
  • 兼容 go get 所支持的版本控制系统:Git, Bzr, HG, and SVN
  • 支持 GO15VENDOREXPERIMENT 特性,使得不同项目可以依赖相同项目的不同版本
  • 可以导入其他工具配置,例如: Godep, GPM, Gom, and GB

安装glide

$ go get github.com/Masterminds/glide$ go install github.com/Masterminds/glide

验证

$ glideNAME:   glide - Vendor Package Management for your Go projects.   Each project should have a 'glide.yaml' file in the project directory. Files   look something like this:       package: github.com/Masterminds/glide       imports:       - package: github.com/Masterminds/cookoo         version: 1.1.0       - package: github.com/kylelemons/go-gypsy         subpackages:         - yaml   For more details on the 'glide.yaml' files see the documentation at   https://glide.sh/docs/glide.yamlUSAGE:   glide [global options] command [command options] [arguments...]VERSION:   0.13.0-devCOMMANDS:     create, init       Initialize a new project, creating a glide.yaml file     config-wizard, cw  Wizard that makes optional suggestions to improve config in a glide.yaml file.     get                Install one or more packages into `vendor/` and add dependency to glide.yaml.     remove, rm         Remove a package from the glide.yaml file, and regenerate the lock file.     import             Import files from other dependency management systems.     name               Print the name of this project.     novendor, nv       List all non-vendor paths in a directory.     rebuild            Rebuild ('go build') the dependencies     install, i         Install a project's dependencies     update, up         Update a project's dependencies     tree               (Deprecated) Tree prints the dependencies of this project as a tree.     list               List prints all dependencies that the present code references.     info               Info prints information about this project     cache-clear, cc    Clears the Glide cache.     about              Learn about Glide     mirror             Manage mirrors     help, h            Shows a list of commands or help for one commandGLOBAL OPTIONS:   --yaml value, -y value  Set a YAML configuration file. (default: "glide.yaml")   --quiet, -q             Quiet (no info or debug messages)   --debug                 Print debug verbose informational messages   --home value            The location of Glide files (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]   --tmp value             The temp directory to use. Defaults to systems temp [$GLIDE_TMP]   --no-color              Turn off colored output for log messages   --help, -h              show help   --version, -v           print the version

看到这样,那就恭喜你,已经安装成功了!!!

使用

篇幅有限,我只介绍经常使用到的。 先进入在GOPATH的一个项目中。

cd $GOPATH/src/foor

初始化 (glide init)

$ glide init[INFO]  Generating a YAML configuration file and guessing the dependencies[INFO]  Attempting to import from other package managers (use --skip-import to skip)[INFO]  Scanning code to look for dependencies[INFO]  --> Found reference to github.com/urfave/cli[INFO]  Writing configuration file (glide.yaml)[INFO]  Would you like Glide to help you find ways to improve your glide.yaml configuration?[INFO]  If you want to revisit this step you can use the config-wizard command at any time.[INFO]  Yes (Y) or No (N)?Y[INFO]  Loading mirrors from mirrors.yaml file[INFO]  Looking for dependencies to make suggestions on[INFO]  --> Scanning for dependencies not using version ranges[INFO]  --> Scanning for dependencies using commit ids[INFO]  Gathering information on each dependency[INFO]  --> This may take a moment. Especially on a codebase with many dependencies[INFO]  --> Gathering release information for dependencies[INFO]  --> Looking for dependency imports where versions are commit ids[INFO]  Here are some suggestions...[INFO]  The package github.com/urfave/cli appears to have Semantic Version releases (http://semver.org).[INFO]  The latest release is v1.19.1. You are currently not using a release. Would you like[INFO]  to use this release? Yes (Y) or No (N)Y[INFO]  Would you like to remember the previous decision and apply it to future[INFO]  dependencies? Yes (Y) or No (N)Y[INFO]  Updating github.com/urfave/cli to use the release v1.19.1 instead of no release[INFO]  The package github.com/urfave/cli appears to use semantic versions (http://semver.org).[INFO]  Would you like to track the latest minor or patch releases (major.minor.patch)?[INFO]  Tracking minor version releases would use '>= 1.19.1, < 2.0.0' ('^1.19.1'). Tracking patch version[INFO]  releases would use '>= 1.19.1, < 1.20.0' ('~1.19.1'). For more information on Glide versions[INFO]  and ranges see https://glide.sh/docs/versions[INFO]  Minor (M), Patch (P), or Skip Ranges (S)?P[INFO]  Would you like to remember the previous decision and apply it to future[INFO]  dependencies? Yes (Y) or No (N)Y[INFO]  Updating github.com/urfave/cli to use the range ~1.19.1 instead of commit id v1.19.1[INFO]  Configuration changes have been made. Would you like to write these[INFO]  changes to your configuration file? Yes (Y) or No (N)Y[INFO]  Writing updates to configuration file (glide.yaml)[INFO]  You can now edit the glide.yaml file.:[INFO]  --> For more information on versions and ranges see https://glide.sh/docs/versions/[INFO]  --> For details on additional metadata see https://glide.sh/docs/glide.yaml/$ llglide.yaml$ cat glide.yaml package: foorimport: []

在初始化过程中, glide 会询问一些问题。 glide.yaml记载了依赖包的列表及其更新规则,每次执行 glide up 时,都会按照指定的规则(如只下载补丁(patch)不下载升级(minor))下载新版。

一个完整的gilde.yaml

package: foorhomepage: https://github.com/qiangmzsxlicense: MITowners: - name: qiangmzsx  email: qiangmzsx@hotmail.com  homepage: https://github.com/qiangmzsx# 去除包ignore:- appengine- golang.org/x/net# 排除目录excludeDirs:- node_modules# 导入包import:- package: github.com/astaxie/beego  version: 1.8.0- package: github.com/coocood/freecache- package: github.com/garyburd/redigo/redis- package: github.com/go-sql-driver/mysql- package: github.com/bitly/go-simplejson- package: git.oschina.net/qiangmzsx/beegofreecachetestImport:- package: github.com/smartystreets/goconvey  subpackages:  - convey

很多人看着yaml很不习惯,没事,我转一下json给大家看看。

{  "excludeDirs": [    "node_modules"  ],   "owners": [    {      "homepage": "https://github.com/qiangmzsx",       "name": "qiangmzsx",       "email": "qiangmzsx@hotmail.com"    }  ],   "license": "MIT",   "package": "foor",   "ignore": [    "appengine",     "golang.org/x/net"  ],   "import": [    {      "version": "1.8.0",       "package": "github.com/astaxie/beego"    },     {      "package": "github.com/coocood/freecache"    },     {      "package": "github.com/garyburd/redigo/redis"    },     {      "package": "github.com/go-sql-driver/mysql"    },     {      "package": "github.com/bitly/go-simplejson"    },     {      "package": "git.oschina.net/qiangmzsx/beegofreecache"    }  ],   "testImport": [    {      "subpackages": [        "convey"      ],       "package": "github.com/smartystreets/goconvey"    }  ],   "homepage": "https://github.com/qiangmzsx"}

版本号指定规则

=: equal (aliased to no operator)!=: not equal>: greater than<: less than>=: greater than or equal to<=: less than or equal to1.2 - 1.4.5 which is equivalent to >= 1.2, <= 1.4.52.3.4 - 4.5 which is equivalent to >= 2.3.4, <= 4.51.2.x is equivalent to >= 1.2.0, < 1.3.0>= 1.2.x is equivalent to >= 1.2.0<= 2.x is equivalent to < 3* is equivalent to >= 0.0.0~1.2.3 is equivalent to >= 1.2.3, < 1.3.0~1 is equivalent to >= 1, < 2~2.3 is equivalent to >= 2.3, < 2.4~1.2.x is equivalent to >= 1.2.0, < 1.3.0~1.x is equivalent to >= 1, < 2^1.2.3 is equivalent to >= 1.2.3, < 2.0.0^1.2.x is equivalent to >= 1.2.0, < 2.0.0^2.3 is equivalent to >= 2.3, < 3^2.x is equivalent to >= 2.0.0, < 3

''指定版本报错,需要用''指定的可以不填写

安装依赖 (glide install)

glide.yaml我们已经准备好了,现在就改安装一下试试。

$ glide install[ERROR]    Failed to parse /home/users/xxxx/golang/src/foor/glide.yaml: yaml: invalid leading UTF-8 octet

报错了!别担心看看你的yaml文件是否为utf-8编码,不是就转换一下就好啦!

$ glide install[INFO]    Lock file (glide.lock) does not exist. Performing update.[INFO]    Downloading dependencies. Please wait...[INFO]    --> Fetching updates for github.com/go-sql-driver/mysql[INFO]    --> Fetching updates for github.com/astaxie/beego[INFO]    --> Fetching updates for github.com/coocood/freecache[INFO]    --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache[INFO]    --> Fetching updates for github.com/bitly/go-simplejson[INFO]    --> Fetching updates for github.com/garyburd/redigo[INFO]    --> Fetching updates for github.com/smartystreets/goconvey[INFO]    --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.0[INFO]    Resolving imports[INFO]    Downloading dependencies. Please wait...[INFO]    Setting references for remaining imports[INFO]    Exporting resolved dependencies...[INFO]    --> Exporting github.com/astaxie/beego[INFO]    --> Exporting github.com/coocood/freecache[INFO]    --> Exporting github.com/bitly/go-simplejson[INFO]    --> Exporting github.com/go-sql-driver/mysql[INFO]    --> Exporting github.com/garyburd/redigo[INFO]    --> Exporting github.com/smartystreets/goconvey[INFO]    --> Exporting git.oschina.net/qiangmzsx/beegofreecache[INFO]    Replacing existing vendor dependencies[INFO]    Project relies on 6 dependencies.$ lltotal 12glide.lockglide.yamlvendor$ ll vendor/git.oschina.netgithub.com

看到glide.look了吗,这个文件记载了依赖包确定的revision, 下次再执行 glide install 时,会直接读这个文件下载确定的版本。

升级版本 (glide up)

glide up 会按照语义化版本规则更新依赖包代码,开发过程中如果需要使用新版代码,可以执行这个命令: 修改一下glide.yaml中的一个Package.

- package: github.com/astaxie/beego  version: 1.8.3

执行glide up。

$ glide up[INFO]    Downloading dependencies. Please wait...[INFO]    --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache[INFO]    --> Fetching updates for github.com/garyburd/redigo[INFO]    --> Fetching updates for github.com/go-sql-driver/mysql[INFO]    --> Fetching updates for github.com/astaxie/beego[INFO]    --> Fetching updates for github.com/bitly/go-simplejson[INFO]    --> Fetching updates for github.com/coocood/freecache[INFO]    --> Fetching updates for github.com/smartystreets/goconvey[INFO]    --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3[INFO]    Resolving imports[INFO]    Downloading dependencies. Please wait...[INFO]    Setting references for remaining imports[INFO]    Exporting resolved dependencies...[INFO]    --> Exporting github.com/astaxie/beego[INFO]    --> Exporting github.com/bitly/go-simplejson[INFO]    --> Exporting github.com/garyburd/redigo[INFO]    --> Exporting github.com/go-sql-driver/mysql[INFO]    --> Exporting github.com/coocood/freecache[INFO]    --> Exporting github.com/smartystreets/goconvey[INFO]    --> Exporting git.oschina.net/qiangmzsx/beegofreecache[INFO]    Replacing existing vendor dependencies[INFO]    Project relies on 6 dependencies.

添加并下载依赖 (glide get)

除了自动从代码中解析 import 外,glide 还可以通过 glide get 直接下载代码中没有的依赖,与 go get 的用法基本一致:

$ glide get github.com/orcaman/concurrent-map[INFO]    Preparing to install 1 package.[INFO]    Attempting to get package github.com/orcaman/concurrent-map[INFO]    --> Gathering release information for github.com/orcaman/concurrent-map[INFO]    --> Adding github.com/orcaman/concurrent-map to your configuration[INFO]    Downloading dependencies. Please wait...[INFO]    --> Fetching updates for github.com/garyburd/redigo[INFO]    --> Fetching updates for github.com/astaxie/beego[INFO]    --> Fetching updates for github.com/go-sql-driver/mysql[INFO]    --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache[INFO]    --> Fetching updates for github.com/bitly/go-simplejson[INFO]    --> Fetching github.com/orcaman/concurrent-map[INFO]    --> Fetching updates for github.com/coocood/freecache[INFO]    --> Fetching updates for github.com/smartystreets/goconvey[INFO]    Resolving imports[INFO]    Downloading dependencies. Please wait...[INFO]    --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3[INFO]    Exporting resolved dependencies...[INFO]    --> Exporting github.com/smartystreets/goconvey[INFO]    --> Exporting github.com/garyburd/redigo[INFO]    --> Exporting github.com/go-sql-driver/mysql[INFO]    --> Exporting github.com/orcaman/concurrent-map[INFO]    --> Exporting github.com/astaxie/beego[INFO]    --> Exporting github.com/bitly/go-simplejson[INFO]    --> Exporting github.com/coocood/freecache[INFO]    --> Exporting git.oschina.net/qiangmzsx/beegofreecache[INFO]    Replacing existing vendor dependencies

使用镜像 (glide mirror)

[WARN]    Unable to checkout golang.org/x/crypto[ERROR]    Update failed for golang.org/x/crypto: Cannot detect VCS[ERROR]    Failed to do initial checkout of config: Cannot detect VCS

这几行信息估计很多人都是遇到过的。在我天朝或者在公司内部都可能不能访问一些站点,导致很Golang的依赖包不能通过go get下载。此时也就是glide大发神威的时候到了,可以通过配置将墙了的版本库 URL 映射到没被墙的 URL,甚至也可以映射到本地版本库。 将golang.org映射到github: 修改glide.yaml加入

- package: golang.org/x/crypto

如果你的网络可以访问就不需要使用glide镜像功能,可以跳过。

$ glide mirror set golang.org/x/crypto github.com/golang/crypto[INFO]    golang.org/x/crypto being set to github.com/golang/crypto[INFO]    mirrors.yaml written with changes$ glide up[INFO]    Loading mirrors from mirrors.yaml file[INFO]    Downloading dependencies. Please wait...[INFO]    --> Fetching updates for github.com/orcaman/concurrent-map[INFO]    --> Fetching golang.org/x/crypto[INFO]    --> Fetching updates for github.com/astaxie/beego[INFO]    --> Fetching updates for github.com/go-sql-driver/mysql[INFO]    --> Fetching updates for github.com/garyburd/redigo[INFO]    --> Fetching updates for github.com/coocood/freecache[INFO]    --> Fetching updates for github.com/bitly/go-simplejson[INFO]    --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache[INFO]    --> Fetching updates for github.com/smartystreets/goconvey[INFO]    --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3[INFO]    Resolving imports[INFO]    Downloading dependencies. Please wait...[INFO]    Setting references for remaining imports[INFO]    Exporting resolved dependencies...[INFO]    --> Exporting github.com/astaxie/beego[INFO]    --> Exporting github.com/coocood/freecache[INFO]    --> Exporting github.com/smartystreets/goconvey[INFO]    --> Exporting github.com/garyburd/redigo[INFO]    --> Exporting github.com/go-sql-driver/mysql[INFO]    --> Exporting github.com/bitly/go-simplejson[INFO]    --> Exporting github.com/orcaman/concurrent-map[INFO]    --> Exporting golang.org/x/crypto[INFO]    --> Exporting git.oschina.net/qiangmzsx/beegofreecache[INFO]    Replacing existing vendor dependencies[INFO]    Project relies on 8 dependencies.$ ll vendor/git.oschina.netgithub.comgolang.org

终于看到golang.org啦!!! 细心的你一定已经发现了

[INFO]    mirrors.yaml written with changes

说明执行glide mirror时候镜像配置写入到的是$HOME/.glide/mirrors.yaml中,打开看看。

repos:- original: golang.org/x/crypto  repo: github.com/golang/crypto

还可以映射到本地目录。 推荐大家可以去https://www.golangtc.com/download/package下载很多Golang类库。 现在我去下载了:https://www.golangtc.com/static/download/packages/golang.org.x.text.tar.gz,解压到本地目录/home/users/qiangmzsx/var/golang/golang.org/x/text

$ glide mirror set golang.org/x/text /home/users/qiangmzsx/var/golang/golang.org/x/text[INFO]    golang.org/x/text being set to /home/users/qiangmzsx/var/golang/golang.org/x/text[INFO]    mirrors.yaml written with changes$ glide up[INFO]    Loading mirrors from mirrors.yaml file[INFO]    Downloading dependencies. Please wait...[INFO]    --> Fetching golang.org/x/text[INFO]    --> Fetching updates for github.com/garyburd/redigo[INFO]    --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache[INFO]    --> Fetching updates for github.com/astaxie/beego[INFO]    --> Fetching updates for github.com/bitly/go-simplejson[INFO]    --> Fetching updates for github.com/go-sql-driver/mysql[INFO]    --> Fetching updates for github.com/coocood/freecache[INFO]    --> Fetching updates for github.com/orcaman/concurrent-map[INFO]    --> Fetching updates for golang.org/x/crypto[INFO]    --> Fetching updates for github.com/smartystreets/goconvey[INFO]    --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3[INFO]    Resolving imports[INFO]    Downloading dependencies. Please wait...[INFO]    Setting references for remaining imports[INFO]    Exporting resolved dependencies...[INFO]    --> Exporting github.com/astaxie/beego[INFO]    --> Exporting github.com/go-sql-driver/mysql[INFO]    --> Exporting github.com/bitly/go-simplejson[INFO]    --> Exporting github.com/coocood/freecache[INFO]    --> Exporting github.com/smartystreets/goconvey[INFO]    --> Exporting github.com/garyburd/redigo[INFO]    --> Exporting github.com/orcaman/concurrent-map[INFO]    --> Exporting golang.org/x/text[INFO]    --> Exporting golang.org/x/crypto[INFO]    --> Exporting git.oschina.net/qiangmzsx/beegofreecache[INFO]    Replacing existing vendor dependencies[INFO]    Project relies on 9 dependencies.

全局选项

运行glide,在最后就可以看到

GLOBAL OPTIONS:   --yaml value, -y value  Set a YAML configuration file. (default: "glide.yaml")   --quiet, -q             Quiet (no info or debug messages)   --debug                 Print debug verbose informational messages   --home value            The location of Glide files (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]   --tmp value             The temp directory to use. Defaults to systems temp [$GLIDE_TMP]   --no-color              Turn off colored output for log messages   --help, -h              show help   --version, -v           print the version

如果大家想把glide的yaml文件换别的默认名称可以执行

 $ glide -y qiangmzsx.yaml

在官网中会看到一个GLIDE_HOME变量,该变量就是/home/users/qiangmzsx/.glide。 这个目录之前有提到过,除了包含有mirrors.yaml还有一个很重要的目录cache本地 cache,每次更新代码时, glide 都会在本地保存 cache,以备下次 glide install 使用 。

GLIDE_HOME可以通过如下命令修改。

 $ glide  --home /home/glide

总结

除了上述说到的功能,glide还有很多好的功能,后续有机会在写出来吧。
总结一下,glide是一款功能丰富,完全满足需求的依赖管理工具,强烈大家使用。

谋胆并重
版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
标签列表