go programming language

Learn from examples

1. Program to output a string, integer, and floating point exgl1.go source code // exgl1.go example print string, integer, float package main import "fmt" // no semicolons func main() { // the { must be here, not on next line fmt.Println("go run exgl1.go running") var msg = "my string" var i = 9999 var x = 37.1 var y = 1.234e+100 fmt.Println(msg) fmt.Println("i= ",i) fmt.Println("x= ",x) fmt.Println("x= ",x,", y= ",y) fmt.Println("exgl1.go finished") } Output from execution: go run exgl1.go running my string i= 9999 x= 37.1 x= 37.1 , y= 1.234e+100 exgl1.go finished 2. commands to execute the source code at a minimum, Windows, Linux, MacOSX. Windows: go run exgl1.go Linux: go run exgl1.go MacOSX go run exgl1.go Sample Makefile: 3. You must be able to declare and use variables, arrays, matrix exgl3.go source code // exgl3.go example declare and use variables, arrays, matrix package main import("fmt") func main() { fmt.Println("go run exgl3.go running") var y float64 y = 123.4 + 0.05 fmt.Println("y=", y) // Here we create an array a that will hold exactly 5 ints. // The type of elements and length are both part of the array’s type. // By default an array is zero-valued, which for ints means 0s. var ai [5]int fmt.Println("array ai:", ai) var x [3]float64 fmt.Println("array x:", x) // We can set a value at an index using the array[index] = value ai[4] = 100 fmt.Println("modified:", ai) fmt.Println("ai[4]=", ai[4]) x[0] = 1.1 x[1] = 2.2 x[2] = 3.3 fmt.Println("array x:", x) // The builtin len returns the length of an array. fmt.Println("len(ai)=", len(ai)) // Use this syntax to declare and initialize an array in one line. b := [5]int{1, 2, 3, 4, 5} fmt.Println("array b:", b) // multi-dimensional data structures. var twoD [2][3]int for i := 0; i <: 2; i++ { for j := 0; j < 3; j++ { twoD[i][j] = i + j } } fmt.Println("twoD= ", twoD) m1 := [2][2]float64{{1.1, 1.2},{2.1, 2.2}} fmt.Println("matrix m1= ", m1) fmt.Println("exgl3.go finished") } Execution output: go run exgl3.go running y= 123.45 array ai: [0 0 0 0 0] array x: [0 0 0] modified: [0 0 0 0 100] ai[4]= 100 array x: [1.1 2.2 3.3] len(ai)= 5 array b: [1 2 3 4 5] twoD= [[0 1 2] [1 2 3]] matrix m1= [[1.1 1.2] [2.1 2.2]] exgl3.go finished 4. You need to be able to have loops, iteration statements exgl4.go source code // exgl4.go example loops, iteration package main import("fmt") func main() { fmt.Println("go run exgl4.go running") i := 1 for i <= 3 { // the { must be here, not on next line fmt.Println(i) i = i + 1 } fmt.Println(" ") for j := 7; j <= 9; j++ { fmt.Println(j) } fmt.Println(" ") for { fmt.Println("loop") break } fmt.Println(" ") for n := 0; n <= 5; n++ { if n%2 == 0 { continue } // end if fmt.Println(n) } // end for n fmt.Println("exgl4.go finished") } Execution output: go run exgl4.go running 1 2 3 7 8 9 loop 1 3 5 exgl4.go finished 5. You need if then else conditional statements exgl5.go source code // exgl5.go example if then else package main import("fmt") func main() { fmt.Println("go run exgl5.go running") // the normal comparison == != >= <= < > if 8%4 == 0 { // the { must be here, not on next line fmt.Println("8 is divisible by 4") } var n = 2 if ((n < 3) && (n > 1)) || n != 3 { fmt.Println("n=", n) } if 7%2 == 0 { fmt.Println("7 is even") } else { // both } and { must be here, not on other lines fmt.Println("7 is odd") } // A statement can precede conditionals; any variables declared // in this statement are available in all branches. num if num := 9; num < 0 { fmt.Println(num, "is negative") } else if num < 10 { // both } and { must be on this line fmt.Println(num, "has 1 digit") } else { fmt.Println(num, "has multiple digits") } fmt.Println("exgl5.go finished") } Execution output: go run exgl5.go running 8 is divisible by 4 n= 2 7 is odd 9 has 1 digit exgl5.go finished 6. You need to be able to create functions, procedures, subroutines. exgl6.go source code // exgl6.go example create and call functions and procedures package main import("fmt") func main() { fmt.Println("go run exgl6.go running") fmt.Println("exgl6.go finished") } Execution output: go run exgl6.go running exgl6.go finished 7. You need to be able to read and write files in various formats. exgl7.go source code // exgl7.go example read and write files in various formats package main import("fmt") func main() { fmt.Println("go run exgl7.go running") fmt.Println("exgl7.go finished") } Execution output: go run exgl7.go running exgl7.go finished 8. You need to be able to use a number of files combined to build a program. This may include packages, libraries, operating system commands, header files, etc. exgl8.go source code // exgl8.go example external files, libraries package main import("fmt") func main() { fmt.Println("go run exgl8.go running") fmt.Println("exgl8.go finished") } Execution output: go run exgl8.go running exgl8.go finished

Last updated 7/25/2019