about untyped type?

60 views
Skip to first unread message

xie cui

unread,
Mar 19, 2021, 8:46:43 AM3/19/21
to golang-nuts
https://github.com/golang/go/blob/master/src/cmd/compile/internal/types2/universe.go#L58-L64
there are some basic type call untyped xxx? what is it mean?
when will go compiler use them? can you show in example?

Jan Mercl

unread,
Mar 19, 2021, 8:54:21 AM3/19/21
to xie cui, golang-nuts
On Fri, Mar 19, 2021 at 1:47 PM xie cui <cuiw...@gmail.com> wrote:

> https://github.com/golang/go/blob/master/src/cmd/compile/internal/types2/universe.go#L58-L64
> there are some basic type call untyped xxx? what is it mean?

Untyped bool, int, etc are the types of untyped constants:
https://golang.org/ref/spec#Constants

> when will go compiler use them?

The compiler already uses them.

> can you show in example?

const i = 42 // i is untyped int
const j = int(42) // j is int

var f float64 = i // ok because i is untyped int
var g float64 = j // compile error, type mismatch

https://play.golang.org/p/pVZyvzLLSXM
Reply all
Reply to author
Forward
0 new messages