Install Golang from here : https://golang.org/doc/install
Print Hello World : (make hello.go file)
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
For Runing Program do go run hello.go in your command line
package main
import "fmt"
func main() {
name := "Quera"
number := 123
fmt.Println(name)
fmt.Println(number)
number = 1234
fmt.Println(number)
}
Output:
Quera
123
1234
For Get Input From User , Use Scan Function From fmt Package
Below Program Get Two Number and Calculate sum of them
package main
import "fmt"
func main() {
var a, b int
fmt.Scan(&a, &b)
fmt.Println(a + b)
}
Below code Detect Number is Odd or Even :
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
if n % 2 == 0 {
fmt.Println("Even")
} else {
fmt.Println("Odd")
}
}
Below code Detect Number is Palindrom Or Not :
// Golang program to check whether a number is palindrome or not
package main
import "fmt"
func main() {
var number,remainder,temp int
var reverse int = 0
fmt.Print("Enter any positive integer : ")
fmt.Scan(&number)
temp=number
// For Loop used in format of While Loop
for{
remainder = number%10
reverse = reverse*10 + remainder
number /= 10
if(number==0){
break // Break Statement used to exit from loop
}
}
if(temp==reverse){
fmt.Printf("%d is a Palindrome",temp)
}else{
fmt.Printf("%d is not a Palindrome",temp)
}
}
All Part of For Loop are Optional
Below Code sum all number form one to n
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
sum := 0
for i := 1; i <= n; i++ {
sum += i
}
fmt.Println(sum)
}
Below Code is equal While Loop in Other Language :
while n!=0 the result is sum of all numbers
package main
import "fmt"
func main() {
var n int
fmt.Scan(&n)
sum := 0
for n != 0 {
sum += n
fmt.Scan(&n)
}
fmt.Println(sum)
}
package main
import "fmt"
func main() {
var a [2]string
a[0] = "Hello"
a[1] = "World"
fmt.Println(a[0], a[1])
fmt.Println(a)
primes := [6]int{2, 3, 5, 7, 11, 13}
fmt.Println(primes)
for i, prime := range primes {
fmt.Println(i, prime)
}
}
OutPut Will Be :
Hello World
[Hello World]
[2 3 5 7 11 13]
0 2
1 3
2 5
3 7
4 11
5 13
calculate average of two decimal number 3 and 6
package main
import "fmt"
func avg(x float64, y float64) float64 {
return (x + y) / 2
}
func main() {
fmt.Println(avg(3, 6))
}