@kjk1/

book typed vs untyped

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.9043144561812875","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"Ly8gYWxsb3cgZXJyb3IKcGFja2FnZSBtYWluCgpmdW5jIG1haW4oKSB7CgkvLyA6c2hvdyBzdGFydAoJY29uc3QgdW50eXBlZE51bWJlciA9IDM0NQoKCS8vIGlmIHRoaXMgd2FzIGEgdmFyaWFibGUgZGVjbGFyYXRpb24sIHVudHlwZWROdW1iZXIgd291bGQgaGF2ZSBpdHMgdHlwZQoJLy8gaW5mZXJyZWQgYXMgaW50LgoJLy8gc2luY2UgaXQncyBhIGNvbnN0LCBpdCByZW1haW5zIHVudHlwZWQgdW50aWwgaXQncyBlLmcuIGFzc2lnbmVkIHRvIGEgdmFyaWFibGUKCXZhciBpIGludCA9IHVudHlwZWROdW1iZXIgICAgICAvLyBubyBuZWVkIHRvIGNhc3QgdG8gaW50Cgl2YXIgdTE2IHVpbnQxNiA9IHVudHlwZWROdW1iZXIgLy8gbm8gbmVlZCB0byBjYXN0IHRvIHVpbnQxNgoJdmFyIGYgZmxvYXQ2NCA9IHVudHlwZWROdW1iZXIgIC8vIG5vIG5lZWQgdG8gY2FzdCB0byBmbG9hdDY0CgoJLy8gaW5jb21wYXRpYmxlIGFzc2lnbm1lbnRzIGFyZSBkZXRlY3RlZCBieSB0aGUgY29tcGlsZXIKCS8vIDM0NSBpcyB0b28gYmlnIHRvIGZpdCBpbiBpbnQ4IGFuZCBjb21waWxlciBkZXRlY3RzIHRoYXQKCXZhciBiIGludDggPSB1bnR5cGVkTnVtYmVyCgoJLy8gOnNob3cgZW5kCgoJLy8gc2lsZW5jZSBjb21waWxlciBlcnJvciBhYm91dCB1bnVzZWQgdmFyaWFibGVzCglfLCBfLCBfLCBfID0gaSwgdTE2LCBmLCBiCn0K"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// allow error
package main

func main() {
	// :show start
	const untypedNumber = 345

	// if this was a variable declaration, untypedNumber would have its type
	// inferred as int.
	// since it's a const, it remains untyped until it's e.g. assigned to a variable
	var i int = untypedNumber      // no need to cast to int
	var u16 uint16 = untypedNumber // no need to cast to uint16
	var f float64 = untypedNumber  // no need to cast to float64

	// incompatible assignments are detected by the compiler
	// 345 is too big to fit in int8 and compiler detects that
	var b int8 = untypedNumber

	// :show end

	// silence compiler error about unused variables
	_, _, _, _ = i, u16, f, b
}