переменные среды

This commit is contained in:
badkaktus 2019-10-12 21:20:47 +03:00
parent 89b09c204a
commit 11c71d2916
3 changed files with 21 additions and 20 deletions

View File

@ -65,7 +65,7 @@ Epoch
Аргументы командной строки (Command-Line Arguments)
Флаги командной строки (Command-Line Flags)
Подкоманды командной строки (Command-Line Subcommands)
Environment Variables
Переменные среды (Environment Variables)
HTTP Clients
HTTP Servers
Spawning Processes

View File

@ -1,7 +1,8 @@
// [Environment variables](http://en.wikipedia.org/wiki/Environment_variable)
// are a universal mechanism for [conveying configuration
// information to Unix programs](http://www.12factor.net/config).
// Let's look at how to set, get, and list environment variables.
// [Переменные среды (или переменные окружения)](http://en.wikipedia.org/wiki/Environment_variable)
// - это [универсальный механизм передачи информации о конфигурации
// в программы Unix](http://www.12factor.net/config). Давайте
// посмотрим, как устанавливать, получать и перечислять переменные
// среды.
package main
@ -13,18 +14,19 @@ import (
func main() {
// To set a key/value pair, use `os.Setenv`. To get a
// value for a key, use `os.Getenv`. This will return
// an empty string if the key isn't present in the
// environment.
// Чтобы установить пару ключ/значение, используйте
// `os.Setenv`. Чтобы получить значение для ключа,
// используйте `os.Getenv`. Это вернет пустую строку,
// если ключ не присутствует в среде.
os.Setenv("FOO", "1")
fmt.Println("FOO:", os.Getenv("FOO"))
fmt.Println("BAR:", os.Getenv("BAR"))
// Use `os.Environ` to list all key/value pairs in the
// environment. This returns a slice of strings in the
// form `KEY=value`. You can `strings.Split` them to
// get the key and value. Here we print all the keys.
// Используйте `os.Environ` для вывода списка всех пар
// ключ/значение в среде. Это возвращает спез строк в
// формате `KEY=value`. Вы можете использовать `strings.Split`,
// чтобы получить ключ и значение. Здесь мы печатаем
// все ключи.
fmt.Println()
for _, e := range os.Environ() {
pair := strings.Split(e, "=")

View File

@ -1,19 +1,18 @@
# Running the program shows that we pick up the value
# for `FOO` that we set in the program, but that
# `BAR` is empty.
# Запуск программы показывает, что мы выбираем значение
# для `FOO`, которое мы установили в программе, но `BAR`
# пуст.
$ go run environment-variables.go
FOO: 1
BAR:
# The list of keys in the environment will depend on your
# particular machine.
# Список ключей в среде будет зависеть от вашей системы.
TERM_PROGRAM
PATH
SHELL
...
# If we set `BAR` in the environment first, the running
# program picks that value up.
# Если мы сначала установим `BAR` в среде, запущенная
# программа использует это значение.
$ BAR=2 go run environment-variables.go
FOO: 1
BAR: 2