Home > Projects > Hello World
Go (often referred to as golang) is programming language created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson.
Go Advantages
For more information on Go visit these links:
Simple Hello World
Go version go1.7.1
package main import "fmt" func main() { fmt.Println("hello world") }
Hello World Ascii Variation
Create a file called gohello.go
Then define two functions, with a for loop statement that repeatedly prints the "#" or "space" characters, until the specified number of repetitions evaluate to false. The parameter specifying the number of times to repeat the "#" or "space" is named a.
Two other functions are defined for when a line return is needed. These are named drawLastPound() and drawLastSpace()
package main import "fmt" func main() { drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(5); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(6); drawPound(3); drawSpace(5); drawPound(1); drawSpace(7); drawPound(1); drawSpace(2); drawPound(3); drawSpace(2); drawPound(4); drawSpace(2); drawPound(1); drawSpace(5); drawPound(4); drawSpace(3); drawLastPound(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(4); drawPound(1); drawSpace(3); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(2); drawLastPound(1); drawPound(5); drawSpace(1); drawPound(5); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(4); drawPound(1); drawSpace(3); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(4); drawSpace(2); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(2); drawLastPound(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(5); drawPound(1); drawSpace(2); drawPound(1); drawSpace(2); drawPound(1); drawSpace(2); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(1); drawSpace(2); drawPound(1); drawSpace(2); drawPound(1); drawSpace(5); drawPound(1); drawSpace(3); drawPound(1); drawSpace(2); drawLastSpace(1); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(5); drawSpace(1); drawPound(5); drawSpace(1); drawPound(5); drawSpace(2); drawPound(3); drawSpace(2); drawPound(1); drawSpace(4); drawPound(1); drawSpace(2); drawPound(1); drawSpace(5); drawPound(3); drawSpace(2); drawPound(1); drawSpace(3); drawPound(1); drawSpace(1); drawPound(5); drawSpace(1); drawPound(4); drawSpace(3); drawLastPound(1); drawSpace(29); drawPound(1); drawSpace(38); drawLastSpace(1); } func drawPound(a int) string { i := 1 for i < = a { fmt.Print("#") i = i + 1 } return "" } func drawSpace(a int) string { i := 1 for i < = a { fmt.Print(" ") i = i + 1 } return "" } func drawLastPound(a int) string { i := 1 for i < = a { fmt.Println("#") i = i + 1 } return "" } func drawLastSpace(a int) string { i := 1 for i < = a { fmt.Println(" ") i = i + 1 } return "" }
Run the file gohello.go:
...\src\github.com\user\gohello>go run gohello.go
# # ##### # # ### # # ### #### # #### #
# # # # # # # # # # # # # # # # # #
##### ##### # # # # # # # # # #### # # # #
# # # # # # # # # # # # # # # # #
# # ##### ##### ##### ### # # # ### # # ##### #### #
#
Go (Two-dimensional Array)
There are better and simpler ways to print Ascii art than this two-dimensional array example. In the past I worked with junior developers who didn't understand arrays. Basically here is the type of example I showed them to illustrate the concept.
Periods were used as placemarkers for the spaces to make it visually easier.
Create a file called gohelloarray.go
Add package main and import fmt which implements formatted I/O with functions analogous to C's printf and scanf.
package main import "fmt" func main() { rows := 5 columns := 11 hello := [5][11]string{}
For the populating the hello array, refer to the illustration above to add elements to it:
//row 1 hello[0][0] = " _ " hello[0][1] = " " hello[0][2] = " _ " hello[0][3] = " _ " hello[0][4] = " " hello[0][5] = " " hello[0][6] = " " hello[0][7] = " " hello[0][8] = " _ " hello[0][9] = " _ " hello[0][10] = " _ " //row 2 hello[1][0] = "| |__ " hello[1][1] = " ___ " hello[1][2] = "| |" hello[1][3] = "| |" hello[1][4] = " ___ " hello[1][5] = " ___ ___" hello[1][6] = " ___ " hello[1][7] = " ___ " hello[1][8] = "| |" hello[1][9] = " __| |" hello[1][10] = "| |"
Note the use of double backslashes on some of the strings where escape characters needed to be used as actual characters in the Ascii art.
(e.g. val str = "\\" instead of "\" )
//row 3 hello[2][0] = "| \\" hello[2][1] = "/ __\\" hello[2][2] = "| |" hello[2][3] = "| |" hello[2][4] = "/ _ \\ " hello[2][5] = " \\ || /" hello[2][6] = "/ _ \\" hello[2][7] = "| _\\" hello[2][8] = "| |" hello[2][9] = "/ _' |" hello[2][10] = "| |" //row 4 hello[3][0] = "|_||_|" hello[3][1] = "\\___|" hello[3][2] = "|_|" hello[3][3] = "|_|" hello[3][4] = "\\___/ " hello[3][5] = "_ \\_/\\_/ " hello[3][6] = "\\___/" hello[3][7] = "|_| " hello[3][8] = "|_|" hello[3][9] = "\\____|" hello[3][10] = "|_|" //row 5 hello[4][0] = " " hello[4][1] = " " hello[4][2] = " " hello[4][3] = " " hello[4][4] = " |/" hello[4][5] = " " hello[4][6] = " " hello[4][7] = " " hello[4][8] = " " hello[4][9] = " " hello[4][10] = "(_)"
Next, iterate over the array with for loops:
var i, j int for i =0; i < rows; i++ { for j = 0; j < columns; j++ { fmt.Print(" ", hello[i][j]) } fmt.Println("") } }
Run the file gohelloarray.go:
...\src\github.com\user\gohelloarray>go run gohelloarray.go
_ _ _ _ _ _
| |__ ___ | || | ___ ___ ___ ___ ___ | | __| || |
| \/ __\| || |/ _ \ \ || // _ \| _\| |/ _' || |
|_||_|\___||_||_|\___/ _ \_/\_/ \___/|_| |_|\____||_|
|/ (_)