GoLang Tutorial - Data Types and Variables
Let's change the "hello.go" so that it uses a variable:
package main import "fmt" func main() { var x string = "Hello World" fmt.Println(x) }
Notice that the string literal from the original program still appears in this program, but rather than send it directly to the Println function we assign it to a variable instead. Variables in Go are created by first using the var keyword, then specifying the variable name (x), the type (string) and finally assigning a value, "Hello World" to the variable.
Note also that we can skip the type string:
var x = "Hello World"
We can split the declaration and definition, and that works as well:
var x string x = "Hello World"
We get an error if a variable is not used after being defined:
Let's try int type:
package main import "fmt" func main() { var x = "Hello World " var year int = 2019 fmt.Println(x, year) }
Output:
Hello World 2019
Again, the go can infer the type, we do not have to explicitly specify the type. So, we can write the code without int:
var year = 2019
We can print out the types of variables using Printf()
and %T
:
Output:
$ go run type.go Type: string, value: Hello World Type: int, value: 2023 Type: int32, value: 2024
"Inside" a function, the := short assignment statement can be used in place of a var declaration with implicit type:
package main import "fmt" func main() { var i, j int = 1, 2 k := 3 c, python, java := true, false, "no!" fmt.Println(i, j, k, c, python, java) fmt.Printf("%T %T %T %T %T %T\n", i, j, k, c, python, java) }
Output:
1 2 3 true false no! int int int bool bool string
However, outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available, and we'll get an error:
We can't use it twice:
legal := 42 legal := 42 // <-- error
That's because, := introduces "a new variable", using it twice does not redeclare a second variable, so it's illegal.
package main import "fmt" var foo int = 34 func main() { foo := 42 // <-- legal foo = 314 // <-- legal fmt.Println(foo) }
Here, foo := 42 is legal, because, it redeclares foo in some() func's scope. Also, foo = 314 is legal, because, it just reassigns a new value to foo.
Go Tutorial
- GoLang Tutorial - HelloWorld
- Calling code in an external package & go.mod / go.sum files
- Workspaces
- Workspaces II
- Visual Studio Code
- Data Types and Variables
- byte and rune
- Packages
- Functions
- Arrays and Slices
- A function taking and returning a slice
- Conditionals
- Loops
- Maps
- Range
- Pointers
- Closures and Anonymous Functions
- Structs and receiver methods
- Value or Pointer Receivers
- Interfaces
- Web Application Part 0 (Introduction)
- Web Application Part 1 (Basic)
- Web Application Part 2 (Using net/http)
- Web Application Part 3 (Adding "edit" capability)
- Web Application Part 4 (Handling non-existent pages and saving pages)
- Web Application Part 5 (Error handling and template caching)
- Web Application Part 6 (Validating the title with a regular expression)
- Web Application Part 7 (Function Literals and Closures)
- Building Docker image and deploying Go application to a Kubernetes cluster (minikube)
- Serverless Framework (Serverless Application Model-SAM)
- Serverless Web API with AWS Lambda
- Arrays vs Slices with an array left rotation sample
- Variadic Functions
- Goroutines
- Channels ("<-")
- Channels ("<-") with Select
- Channels ("<-") with worker pools
- Defer
- GoLang Panic and Recover
- String Formatting
- JSON
- SQLite
- Modules 0: Using External Go Modules from GitHub
- Modules 1 (Creating a new module)
- Modules 2 (Adding Dependencies)
- AWS SDK for Go (S3 listing)
- Linked List
- Binary Search Tree (BST) Part 1 (Tree/Node structs with insert and print functions)
- Go Application Authentication I (BasicAuth, Bearer-Token-Based Authentication)
- Go Application Authentication II (JWT Authentication)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization