The Go language is very similar to C. Here are some simple programs.
Let’s begin… Calculate 2 to the 12th power.
package main import "fmt" func main () { a := 2 << 11 // or use this: math.Pow(2, 12) fmt.Println(a) }
Generate a random number between 1 and 21
package main import ("fmt"; "math/rand"; "time") func main () { rand.Seed(time.Now().UnixNano()) a := rand.Intn(21) + 1 // from 1 to 21 fmt.Println(a) }
Now lets check examples from the book “An Introduction to Programming in Go” by Caleb Doxsey:
Temperature conversion from degrees Fahrenheit to Celsius (formula (F - 32) * 5/9
)
package main import "fmt" func main () { var input float64 fmt.Print("Enter temperature in F: ") fmt.Scanf("%f", &input) input = (input - 32) * 5/9 fmt.Println("It will be", input, "Celsius") }
Translation of feet to meters (1 f = 0.3048 m).
package main import "fmt" func main () { var input float64 fmt.Print("Enter lenght in feet: ") fmt.Scanf("%f", &input) input *= 0.3048 fmt.Println("It will be", input, "meters") }
Output numbers from 1 to 100 that are divisible by 3 (3, 6, 9, 12, …).
package main import "fmt" func main () { for i := 1; i <= 100; i++ { if i % 3 == 0 { fmt.Println(i) } } }
Output numbers from 1 to 100, but for multiples of three you must output “Fizz” instead of a number, for multiples of five – “Buzz”, and for multiples of both three and five – “FizzBuzz”.
package main import "fmt" func main () { for i := 1; i <= 100; i++ { if i % 3 == 0 && i % 5 == 0 { fmt.Println("FizzBuzz") } else if i % 3 == 0 { fmt.Println("Fizz") } else if i % 5 == 0 { fmt.Println("Buzz") } else { fmt.Println(i) } } }
By the way, you can output the first condition as %15 or concatenate 🙂
Print all letters from A to Z
package main import ( "fmt" ) func main() { for i := 0; i < 26; i++ { fmt.Printf("%c", 65+i) } }
The next task is as follows: find the smallest element in the list
x := []int{ 48,96,86,68, 57,82,63,70, 37,34,83,27, 93,94,99,97, }
Solution:
package main import "fmt" func main() { x := []int{ 48,96,86,68, 57,82,63,70, 37,34,83,27, 93,94,99,97, } tiny := x[0] for _, n := range x { if n < tiny { tiny = n } } fmt.Println(tiny) }
Also you can use lib sort.Ints()
🙂
Next… Write a sum function that would take a slice of the numbers and add them together.
package main import "fmt" func sum(nums ... int) int { total := 0 for _, i := range nums { total += i } return total } func main() { slice := []int{1, 3, 5, 7} x := sum(slice...) fmt.Println(x) }
Write a function that takes an integer and divides it in half and returns true if it was even, or false if it was odd. For example, half(1) should return (0, false), and half(2) should return (1, true).
package main import "fmt" func split (i int) (int, bool) { if i % 2 == 0 { return 1, true } else { return 0, false } } func main () { var input int fmt.Print("Enter number to split it and check is it even/odd: ") fmt.Scanf("%d", &input) fmt.Println(split(input)) }
Write a function with a variable number of parameters that finds the largest number in the list.
package main import "fmt" func big (nums ... int) int { x := 0 for _, i := range nums { if i > x { x = i } } return x } func main () { slice := [] int {11, 1, 111, 12, 22, 32} fmt.Println(big(slice ...)) }
Using the makeEvenGenerator function as an example, write a makeOddGenerator that generates odd numbers. Here is the makeEvenGenerator function:
func makeEvenGenerator() func() uint { i := uint(0) return func() (ret uint) { ret = i i += 2 return } } func main() { nextEven := makeEvenGenerator() fmt.Println(nextEven()) // 0 fmt.Println(nextEven()) // 2 fmt.Println(nextEven()) // 4 }
Solution: change the variable at the beginning of the function
Swap two numbers. For example, x := 1; y := 2; swap(&x, &y) should give x = 2 and y = 1.
package main import ( "fmt" ) func swap (x *int, y *int) { tmp := *x *x = *y *y = tmp // in Go you can do this: *x, *y = *y, *x } func main() { x := 1 y := 2 swap(&x, &y) fmt.Println(x, y) }
Sum the digits of a given number, eg 12345 sum will be 15 (1+2+3+4+5)
package main import "fmt" func main() { n := 12345 var sum int for n > 0 { sum += n % 10 n /= 10 } fmt.Println(sum) }
Program which repeats your input (like echo in mountains)
package main import ( "fmt" ) func main() { var s string for { fmt.Scanf("%s", &s) if s != "" { fmt.Println("... ... ", s) } if s == "exit" { break } s = "" } }