Skip to content

Commit

Permalink
included several examples
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpragier committed Dec 11, 2019
1 parent a7756d4 commit 3124c86
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ Topics
- Constants
- Comments
- Basic Types
- Arrays Rudiments
- Structs
- Literal strings
- Flow Control
- Style
- Basic date/time handling
- Anonym Routine / Closure
- Package
- Basic Text Disk I/O
- Basic Test

GO 1.13 ( and later ) is required
149 changes: 136 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ func init() {
// variablesAndSimpleTypes demonstrates all the possible ways to declare and initialize
// variables and constants of simple ( not struct ) types
func variablesAndSimpleTypes() {
const strConst = "string constant"
// Constants should be declared and initialized at once

// Here goes a <uint64> type coertion.
// in this case, if you don't specify type, the compiler would define as <int>
const answer uint64 = 42 // uint64 constant

// Automatic inference
const (
pi = 3.1415926535 // float64 constant
answer uint64 = 42 // uint64 constant
tab = '\t' // use single quote to declare rune
intVar = 0b00001111 // int variable declared as binary literal
pi = 3.1415926535 // float64 constant
tab = '\t' // use single quote to declare rune
strConst = "string constant"
// The lines above only executes from GO 1.13 ( and later )
intVar = 0b00001111 // int variable declared as binary literal
integerHexadecimal = 0x9a
)

var strA = "string variable declared with 'var' keyword"
Expand All @@ -37,14 +44,17 @@ func variablesAndSimpleTypes() {
// Declaring one or more variables in the same instruction block
// The same can be done with "const" declarations
var (
strD string = `literal string containing "double quoted substring" and not subject to escape characters like \t or \n`
intA = 0 // int by inference
intB uint8 // declared as eight bits unsigned int, initialized with zero
fltA = 12.5 // float64 type is standard for floating point values
fltB = float32(9) / float32(2) // here we're forcing float32 type
int9By2 = 9 / 2 // Gotcha! Here the result is integer.
runeA = 'A'
runeB rune = 66
strD string = `literal string containing "double quoted substring" and not subject to escape characters like \t or \n`
intA = 0 // int by inference
intB uint8 // declared as eight bits unsigned int, initialized with zero
fltA = 12.5 // float64 type is standard for floating point values
fltB = float32(9) / float32(2) // here we're forcing float32 type
int9By2 = 9 / 2 // Gotcha! Here the result is integer.
runeA = 'A'
runeB rune = 66
octalInt = 0o660
hexadecimalFloatingPoint = 0x123p8
laterVariableUsingAPreviousValue = strD
)

// This is the standard/embedded function printLn(), to print content to stdout
Expand Down Expand Up @@ -76,6 +86,14 @@ func variablesAndSimpleTypes() {

fmt.Printf("int9By2 is type %T and value %#v\n", int9By2, int9By2)

fmt.Printf("integerHexadecimal is type %T and value %d\n", integerHexadecimal, integerHexadecimal)

fmt.Printf("octalInt is type %T and value %d\n", octalInt, octalInt)

fmt.Printf("hexadecimalFloatingPoint is type %T and value %d\n", hexadecimalFloatingPoint, hexadecimalFloatingPoint)

fmt.Printf("laterVariableUsingAPreviousValue is type %T and value %#v\n", laterVariableUsingAPreviousValue, laterVariableUsingAPreviousValue)

// printLn is a built-in routine
println("Rune is an alias for type int32")

Expand All @@ -84,6 +102,109 @@ func variablesAndSimpleTypes() {
fmt.Printf("runeB is type %T and value %c\n", runeB, runeB)
}

func goStructs() {
// Type Aliasing. We are not creating our type, but renaming an existing one.
type trueOrFalse bool

// A simple struct using the custom type/alias above
type nestedType struct {
float64Field float64
booleanField trueOrFalse
}

// A struct using the simple struct above
type myStruct struct {
intField int
stringField string
nt nestedType
}

// Aliasing our custom type
type myStructTypeAlias myStruct

// We CANNOT use structs for constants.
// Structs initializers are not considerated valid constant values
//const complexConst = nestedType{
// float64Field: 0,
// booleanField: false,
//}

var (
// Declaring and initializing a simple aliased type
tf = trueOrFalse(true)

// declaring variable of aliased type nestedType, which is a valid type for variables
nestedTypeVar nestedType

myStructVar = myStruct{
intField: 15,
stringField: "show me the code",
nt: nestedType{
float64Field: 161718.1920,
booleanField: tf,
},
}

anotherMyStruct = myStructTypeAlias{intField: 15,
stringField: "show me the code",
nt: nestedType{
float64Field: 161718.1920,
booleanField: tf,
},
}
)

var newStruct struct {
X byte
Y uint8
}

newStruct.X = 255

newStruct.Y = newStruct.X

anotherStruct := struct {
SingleField string
}{"anonymous struct declared and initialized"}

var structArray = []struct {
s string
i int
}{
{s: "abc", i: 1},
{s: "def", i: 2},
{s: "ghi", i: 3}, // See this comma? It's mandatory when the block/curly brackets/mustache closes on another line
}

type structA struct {
x int
}

type structB struct {
y structA
}

var nestedStructs = struct {
z structB
}{z: structB{y: structA{0}}} // Notice that here we entirely ignored commas, once the entire initializator in inline

nestedStructs.z.y.x = 15

fmt.Printf("tf is type %T and value %#v\n", tf, tf)

fmt.Printf("nestedTypeVar is type %T and value %#v\n", nestedTypeVar, nestedTypeVar)

fmt.Printf("myStructVar is type %T and value %#v\n", myStructVar, myStructVar)

fmt.Printf("anotherMyStruct is type %T and value %#v\n", anotherMyStruct, anotherMyStruct)

fmt.Printf("anotherStruct is type %T and value %#v\n", anotherStruct, anotherStruct)

fmt.Printf("structArray is type %T and value %#v\n", structArray, structArray)

fmt.Printf("nestedStructs is type %T and value %#v\n", nestedStructs, nestedStructs)
}

func deferredRoutine() {
fmt.Println("this is a deferred routine. Was scheduled by GO compiler to run after the last synchronous instruction end")
fmt.Println("it's not the same as OOP destructor, but it's often used the same way of, to close/dispose allocated resources")
Expand All @@ -110,6 +231,8 @@ func packagedRoutine() {
func main() {
variablesAndSimpleTypes()

goStructs()

defer deferredRoutine()

closureRoutines()
Expand Down

0 comments on commit 3124c86

Please sign in to comment.