Go Concepts - The Type System: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
No edit summary
Line 28: Line 28:
</blockquote>
</blockquote>


=Value and Reference Types=
=User-Defined Types=


=Zero Value=
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[Go Structs|structs]]
</blockquote>


''Zero value'' for a specific type: 0 for <tt>int</tt>s, 0.0 for <tt>float</tt>s, "" for <tt>string</tt>, <tt>false</tt> for Booleans and <tt>nil</tt> for pointers. For reference types, their underlying data structures are initialized to their zero values.


=User-Defined Types=
==Interfaces==


==struct==
<font color=red>Interfaces are not types.</font>


<font color=red>Are all users can define (in terms of types) structs, or there are other user-defined types?</font>
<font color=red>Can only structs be interfaces, or there are other things that can be interfaces?</font>


===Fields===


A field is always [[Go Concepts - Packages#Exported_Identifiers|exported]] by the package it is enclosed in.


==Interfaces==
=Value and Reference Types=


<font color=red>Interfaces are not types.</font>
=Zero Value=


<font color=red>Can only structs be interfaces, or there are other things that can be interfaces?</font>
''Zero value'' for a specific type: 0 for <tt>int</tt>s, 0.0 for <tt>float</tt>s, "" for <tt>string</tt>, <tt>false</tt> for Booleans and <tt>nil</tt> for pointers. For reference types, their underlying data structures are initialized to their zero values.


=Reference Types=
=Reference Types=

Revision as of 17:24, 22 March 2016

Internal

Overview

Go is statically typed. Go designers tried to alleviate some of the "heaviness" associated with statically typed languages and made it "feel" like a dynamic language. For example Go uses local type inference, which eliminates the need to specify the type unnecessarily in program, the compiler figures it out.

Go is strongly typed meaning that yes cannot be unsafely coerced into other types they're not, or at least without programmer giving explicit permission. In JavaScript, for example, implicit conversion is done based on complicated rules that are not always easy to remember.

For more details on typing, see static typing vs. dynamic typing and strong typing vs. loose typing.

Type Definition

Type Definition

Built-in Types

Integers
Floating-Point Numbers
String
Arrays
Slices
Maps

User-Defined Types

structs


Interfaces

Interfaces are not types.

Can only structs be interfaces, or there are other things that can be interfaces?


Value and Reference Types

Zero Value

Zero value for a specific type: 0 for ints, 0.0 for floats, "" for string, false for Booleans and nil for pointers. For reference types, their underlying data structures are initialized to their zero values.

Reference Types

Conversion Between Types

Primitive vs. Non-Primitive Nature

Duck Typing

For more details on duck typing go here.