当前位置: 首页 > 资讯 >

[Golang] Introduction to Variables

Decaliring Variables

Let's update the hello.go example with a variable as following.

package main

import "fmt"

func main() {
    var message string = "Hello world!"
    fmt.Println(message)
}

A variable declairation is composed of 3 components:

var hello string
Inform Go compier that we are creating a new variable the name of the new variable The associated data type.

Go is Static Tye language. It cares the type of value that is going to be assigned to a variable. You can not assign a different type of value to a variable. for example:

var message string = "Hello world"
message = 100 // ERROR! You can not assign a integer value to a string variable

Default Value

Any variable declared without an initial value will have a default value assigned.

Type Default Value
bool false
string ""
int, int32, int64 0
flost32, float64 0.0
package main

import "fmt"

func main() {
	var message string
	fmt.Println(message)

	message = "Hello world!"
	fmt.Println(message)
}

Type Inference

Although Go is a Statically typed language, you do not need to specify the type of every variable you declare.
You can use := to create a new varibale.
Go compiler will analysis the type of value and define the varible type as same as the value.

package main

import "fmt"

func main() {
	// var hello string = "Hello world"
	message := "Hello World"
	message = "Hello Taiwan!"
	fmt.Println(hello)
}

Name Convention

The convention in Go is to use MixedCaps or mixedCaps (simply camelCase) rather than underscores to write multi-word names.

Convention Usage
MixedCaps If an identifier needs to be visible outside the package
mixedCaps If you don't have the intention to use it in another package

Type Conversion

Numeric types conversion

Golang doesn’t allow you to mix numeric types in an expression. For example:

a := 2021
b := 3.1415

result := a + b // Error (invalid operation: mismatched types int and float64)

result := float64(a) + b // Works

String and other basic data types conversion:

Use the strconv package to conversions to/from string representations of basic data types

package main

import (
	"fmt"
	"reflect"
	"strconv"
)

func main() {
	// Convert String to Bool, Float, Integer
	b, err := strconv.ParseBool("true")
	fmt.Println(b, err, reflect.TypeOf(b))

	f, err := strconv.ParseFloat("3.1415", 64)
	fmt.Println(f, err, reflect.TypeOf(f))

	i, err := strconv.ParseInt("-42", 10, 64)
	fmt.Println(i, err, reflect.TypeOf(i))

	// Convert Bool, Float, Integer to String.
	sb := strconv.FormatBool(b)
	sf := strconv.FormatFloat(f, 'G', -1, 64)
	si := strconv.FormatInt(i, 10)

	fmt.Printf(sb + "\t" + sf + "\t" + si + "\n")
}

Constants Variables

We can declair constant variables to represent fixed values

for example

const capital string = "Taipei"
const population2020 int32 = 23568378
const (
	a = 5 + population2020 // Valid
	b = population2020 / 5 // Valid
)

func main() {
	fmt.Printf("Hello %s !\n", capital)
	fmt.Printf("Taiwan's Population: %d!\n", population2020)
}

Access Environment Variables

https://www.callicoder.com/go-read-write-environment-variables/

Sometime, we need to access the system environment variable at runtine so that we can make the same application work in different environments like Dev, UAT, and Production.

The os packages provide functions to work with environment variables, such as:

Funtion Usage
Setenv(key, value) Set an environment variable.
Getenv(key) Get an environment variable.
Unsetenv(key) Unset an environment variable.
LookupEnv(key) Get the value of an environment variable and a boolean indicating whether the environment variable is present or not. It returns a string and a boolean

for example:

package main

import (
	"fmt"
	"os"
)

func main() {
	// Set Environment Variables
	os.Setenv("DB_HOST", "localhost")
	os.Setenv("DB_USER", "admin")
	os.Setenv("DB_PASS", "test123")

	// Get Environment Variables
	user := os.Getenv("DB_USER")
	pass := os.Getenv("DB_PASS")
	fmt.Printf("Host: %s, Port: %s\n", user, pass)

	// Unset Environment Variable
	os.Unsetenv("DB_HOST")
	fmt.Printf("Try to get host: %s\n", os.Getenv("DB_HOST"))


	// Get the value of an environment variable and a boolean indicating whether the environment variable is set or not.
	database, ok := os.LookupEnv("DB_NAME")
	if !ok {
		fmt.Println("DB_NAME is not present")
	} else {
		fmt.Printf("Database Name: %s\n", database)
	}
}


相关文章:

  • [Day11] Flutter with GetX flutter toast & Overlay
  • Day22 ( 游戏设计 ) 小玛莉游戏机
  • Day-23 Toast
  • Alpine Linux Porting (一点十?)
  • [Day26]ISO 27001 附录 A.14 系统获取、开发及维护
  • [Day4] 过年没钱发红包? 那就做一个手机版刮刮乐吧!
  • 切入的点好,就会好点
  • 谁比谁长,回圈和 reduce 用法,Ruby 30 天刷题修行篇第十话
  • 结语: AI平台的六个ING
  • 【Day 9】预训练任务大改:Splinter在QA任务上的成功尝试
  • 第六章 之七
  • Day 11:加入简单的控制项
  • TypeOrm | Repository APIs 用法纪录 3
  • 日记18
  • python入门学习day 4
  • WordPress教程:教你如何置顶文章
  • 2020最新Google Voice号码申请方法(非脚本)
  • WordPress 5.7 引入新钩子,用于过滤单个区块的内容
  • 微信小程序搭建教程:怎么用CentOS搭建小程序服务器
  • 挖矿是什么?怎么挖矿怎么挖比特币?虚拟币比特币挖矿原理
  • 一键脚本:SmokePing一键安装/管理脚本
  • 升级wordpress出错怎么办?wordpress升级502错误解决方法
  • PHP安装环境怎么搭建(LAMP环境怎么搭建):教你搭建PHP+Mysql服务器环境
  • WordPress主题怎么安装?WordPress主题模板安装失败怎么解决
  • 香港电话卡怎么在内地使用: CSL Hello/Three/CMHK/Smartone电话卡内地使用方法
  • 以太坊是什么?以太坊原理和以太坊入门教程
  • 移动怎么开通香港电话号码
  • 教程/Directory Lister逗比魔改版 Github开源(个人云)
  • 一周要闻:谷歌母公司、Facebook、亚马逊等几大互联网公司一季度财报
  • Facebook和instagram推广营销教程