title: What is the role of 'direct' in the GOPROXY variable
date: 2021-11-17 08:53:00
toc: true
category:
- Golang
tags: - golang
- Golang
- repost
- Go
- go
- goproxy
- GOPROXY
- direct
- role
- variable
This article is reposted from: What is the role of 'direct' in the GOPROXY variable · Issue #21 · goproxy/goproxy
Currently, when using this project to build a Go proxy, I found that if the GORPOXY variable is set to https://goproxy.cn,direct, the packages under golang.org will not go through the proxy but will directly time out, while removing 'direct' will work normally. The following is the code for building the proxy program. Did I do something wrong?
The purpose of the comma-separated list in the GOPROXY
environment variable is to automatically fallback to the next one if the previous one responds with 404 or 410. The purpose of 'direct' is to directly fetch the code from the source (such as GitHub, a code hosting service).
If you have internal modules in your company that need to be served, for example, by specifying through environment variables like GOPRIVATE
, then you must include 'direct' in GOPROXY
, otherwise GOPRIVATE
and similar settings will not work properly.
In addition, there is indeed a problem with your environment variable configuration, mainly with the https_proxy
environment variable being configured incorrectly. If you have configured https_proxy
, the Go HTTP client will prioritize its use over http_proxy
. In this case, when using https_proxy
, it will assume that its value is a URL that can be accessed via HTTPS with a valid signed TLS certificate. However, your URL starts with http://
, which is incorrect. In other words, if you configure it like this, all requests made by the Go HTTP client will fail because it cannot even complete the TLS handshake stage. I mentioned this before in goproxy/goproxy.cn#71 (comment).