gobyexample/public/environment-variables
Hana 9e216da9ef go.mod: add go.mod and move pygments to third_party
After go1.16, go will use module mode by default,
even when the repository is checked out under GOPATH
or in a one-off directory. Add go.mod, go.sum to keep
this repo buildable without opting out of the module
mode.

> go mod init github.com/mmcgrana/gobyexample
> go mod tidy
> go mod vendor

In module mode, the 'vendor' directory is special
and its contents will be actively maintained by the
go command. pygments aren't the dependency the go will
know about, so it will delete the contents from vendor
directory. Move it to `third_party` directory now.

And, vendor the blackfriday package.

Note: the tutorial contents are not affected by the
change in go1.16 because all the examples in this
tutorial ask users to run the go command with the
explicit list of files to be compiled (e.g.
`go run hello-world.go` or `go build command-line-arguments.go`).
When the source list is provided, the go command does
not have to compute the build list and whether it's
running in GOPATH mode or module mode becomes irrelevant.
2021-02-15 16:45:26 -05:00

194 lines
7.5 KiB
Plaintext
Generated

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example: Environment Variables</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'command-line-subcommands';
}
if (e.key == "ArrowRight") {
window.location.href = 'http-clients';
}
}
</script>
<body>
<div class="example" id="environment-variables">
<h2><a href="./">Go by Example</a>: Environment Variables</h2>
<table>
<tr>
<td class="docs">
<p><a href="http://en.wikipedia.org/wiki/Environment_variable">Environment variables</a>
are a universal mechanism for <a href="http://www.12factor.net/config">conveying configuration
information to Unix programs</a>.
Let&rsquo;s look at how to set, get, and list environment variables.</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="http://play.golang.org/p/KuD8tDyB4lQ"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="p">(</span>
<span class="s">&quot;fmt&quot;</span>
<span class="s">&quot;os&quot;</span>
<span class="s">&quot;strings&quot;</span>
<span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>To set a key/value pair, use <code>os.Setenv</code>. To get a
value for a key, use <code>os.Getenv</code>. This will return
an empty string if the key isn&rsquo;t present in the
environment.</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">os</span><span class="p">.</span><span class="nx">Setenv</span><span class="p">(</span><span class="s">&quot;FOO&quot;</span><span class="p">,</span> <span class="s">&quot;1&quot;</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;FOO:&quot;</span><span class="p">,</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Getenv</span><span class="p">(</span><span class="s">&quot;FOO&quot;</span><span class="p">))</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">&quot;BAR:&quot;</span><span class="p">,</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Getenv</span><span class="p">(</span><span class="s">&quot;BAR&quot;</span><span class="p">))</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Use <code>os.Environ</code> to list all key/value pairs in the
environment. This returns a slice of strings in the
form <code>KEY=value</code>. You can <code>strings.SplitN</code> them to
get the key and value. Here we print all the keys.</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">()</span>
<span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Environ</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">pair</span> <span class="o">:=</span> <span class="nx">strings</span><span class="p">.</span><span class="nx">SplitN</span><span class="p">(</span><span class="nx">e</span><span class="p">,</span> <span class="s">&quot;=&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">pair</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>Running the program shows that we pick up the value
for <code>FOO</code> that we set in the program, but that
<code>BAR</code> is empty.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> go run environment-variables.go
<span class="go">FOO: 1</span>
<span class="go">BAR: </span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>The list of keys in the environment will depend on your
particular machine.</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="go">TERM_PROGRAM</span>
<span class="go">PATH</span>
<span class="go">SHELL</span>
<span class="go">...</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>If we set <code>BAR</code> in the environment first, the running
program picks that value up.</p>
</td>
<td class="code">
<div class="highlight"><pre><span class="gp">$</span> <span class="nv">BAR</span><span class="o">=</span><span class="m">2</span> go run environment-variables.go
<span class="go">FOO: 1</span>
<span class="go">BAR: 2</span>
<span class="go">...</span>
</pre></div>
</td>
</tr>
</table>
<p class="next">
Next example: <a href="http-clients">HTTP Clients</a>.
</p>
<p class="footer">
by <a href="https://markmcgranaghan.com">Mark McGranaghan</a> | <a href="https://github.com/mmcgrana/gobyexample/blob/master/examples/environment-variables">source</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"os\"\u000A \"strings\"\u000A)\u000A');codeLines.push('func main() {\u000A');codeLines.push(' os.Setenv(\"FOO\", \"1\")\u000A fmt.Println(\"FOO:\", os.Getenv(\"FOO\"))\u000A fmt.Println(\"BAR:\", os.Getenv(\"BAR\"))\u000A');codeLines.push(' fmt.Println()\u000A for _, e :\u003D range os.Environ() {\u000A pair :\u003D strings.SplitN(e, \"\u003D\", 2)\u000A fmt.Println(pair[0])\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>