@kjk1/

book type casting basics

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.6564110593889931","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"Ly8gbm8gb3V0cHV0CnBhY2thZ2UgbWFpbgoKZnVuYyBtYWluKCkgewoJLy8gOnNob3cgc3RhcnQKCS8vIHlvdSBjYW4gY2FzdCBiZXR3ZWVuIG51bWJlcnMgaS5lLiBpbnRlZ2VycyBvZiB2YXJpb3VzIHNpemVzIGFuZCBmbG9hdGluZyBwb2ludCBudW1iZXJzCgl2YXIgaTEgaW50MzIgPSAzCgl2YXIgaTIgaW50ID0gaW50KGkxKSAvLyB3ZSBtdXN0IGV4cGxpY2l0bHkgY2FzdCBmcm9tIGludDMyIHRvIGludAoJdmFyIGYgZmxvYXQ2NCA9IGZsb2F0NjQoaTEpCgoJcyA6PSAic3RyaW5nIgoJLy8gd2UgY2FuIGNhc3QgYmV0d2VlbiBzdHJpbmcgYW5kIFtdYnl0ZSBhbmQgdmljZS12ZXJzYQoJLy8gbm90ZSB0aGF0IHVubGVzcyBvcHRpbWl6dGVkIGJ5IHRoZSBjb21waWxlciwgdGhpcyBpbnZvbHZlcyBhbGxvY2F0aW9uCgl2YXIgZCBbXWJ5dGUgPSBbXWJ5dGUocykKCS8vIDpzaG93IGVuZAoKCV8sIF8sIF8gPSBpMiwgZiwgZCAvLyBzaWxlbmNlIGNvbXBpbGVyIGVycm9yIGFib3V0IHVudXNlZCB2YXJpYWJsZXMKfQo="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// no output
package main

func main() {
	// :show start
	// you can cast between numbers i.e. integers of various sizes and floating point numbers
	var i1 int32 = 3
	var i2 int = int(i1) // we must explicitly cast from int32 to int
	var f float64 = float64(i1)

	s := "string"
	// we can cast between string and []byte and vice-versa
	// note that unless optimizted by the compiler, this involves allocation
	var d []byte = []byte(s)
	// :show end

	_, _, _ = i2, f, d // silence compiler error about unused variables
}