Объявление переменных в Go

Содержание
Введение
Сперва объявление затем инициализация
Объявляем и инициализируем в одной строке
implicit initialization: Go сам присваивает тип
Несколько переменных в одной строке
Ошибки

Введение

В Go переменные можно объявлять разными способами.

package main import "fmt" func main() { // 1. Сперва объявление затем инициализация var i int i = 12 fmt.Println(i) // 2. Объявляем и инициализируем в одной строке var p float32 = 3.14 // 3. Go сам присваивает тип (implicit initialization) site := "HeiHei.ru" = 3.14 fmt.Println(site) // 4. Несколько переменных в одной строке var_complex128 := complex(2, 3) r, im := real(var_complex128), imag(var_complex128) }

go run github/youraccoutn/projectname

12
3.14
HeiHei.ru

Ошибки

Если объявить переменную и не использовать

./main.go:10:6: f declared but not used

Если объявить переменную повторно

./main.go:32:7: no new variables on left side of :=

Article author: Andrei Olegovich

Похожие статьи
Программирование на Go
Объявление переменных
Определить тип переменной
Указатели
Константы
Сертификаты
Запросы к REST API на Go
Установка в Linux

Search this site

Subscribe to Telegram channel @aofeed to follow new articles and updates to old ones

Go to channel

@aofeed

Ask a question in the Telegram group

@aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: