Go, also known as Golang, is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is syntactically similar to C, but with memory safety, garbage collection, structural typing, and CSP-style concurrency.
-
-
Save nehavatsyan/e24f9f22f0870c62de9b7128a5fa16f4 to your computer and use it in GitHub Desktop.
package main | |
import "fmt" | |
func main() { | |
fmt.Printf("hello, world\n") | |
} |
A Go workspace directory must have three sub-directories viz.
- src
- pkg
- bin
credits : https://medium.com/rungo/working-in-go-workspace-3b0576e0534a https://golang.org/doc/code.html#Organization
The src directory contains Go packages. A package in nutshell is a project directory containing Go source code (.go files). Any packages installed using go get command will reside here as well (and its dependency packages). In Go, every program is contained in a package. Hence, whenever you will be working with a new Go project, you need to create a new directory inside $GOPATH/src and work your way up from there.
The pkg directory contains Go package objects. They are the compiled versions of the original package source code and they have .a file extension (a stands for archived). These files contain the compiled package binary code, along with debugging symbols and source information. A package object is customized for machine architecture and the Go version. These files are created by the Go pack tool while building/installing a package.
The bin directory contains the same binary executable files. These files are created by go install commands. go install command runs go build command internally and then outputs these files to the bin directory.
- The Print() function simply prints a list of variables to STDOUT with default formatting.
- The Printf() function allows you to specify the formatting using a format template.
- The Println() function works like Print() except it inserts spaces between the variables and appends a new line at the end.
- %v the value in a default format
- %T a Go-syntax representation of the type of the value
- %d prints an integer in base-10
- %x and %X print an integer in base-16
- %f prints a floating point number without an exponent.
- %t the word true or false
- %o base 8
- %b base 2
- %q a double-quoted string safely escaped with Go syntax
- %p prints a pointer address of your variable.
- The Scan() function reads in space-delimited values into variables
- The Scanf() does the same but lets you specify formatting options using a format string.
- The Scanln() function works Scan() except that it does not treat newlines as spaces.
Credits : https://golang.org/pkg/fmt/ https://medium.com/go-walkthrough/go-walkthrough-fmt-55a14bbbfc53
package main | |
import ( | |
"fmt" | |
) | |
func factorial (n int) (fact int){ | |
if n==0 { | |
fact=1 | |
}else if n<0 { | |
fmt.Println("**** Factorial does not exist ****") | |
}else { | |
fact = n*factorial(n-1) | |
} | |
return fact; | |
} | |
func main() { | |
var n int | |
fmt.Println("Please enter a number 1-10") | |
fmt.Scan(&n) | |
fmt.Println("factorial is ---- ", factorial(n)) | |
} |
// solved problem https://www.hackerearth.com/practice/data-structures/arrays/1-d/practice-problems/algorithm/monk-and-welcome-problem/ | |
package main | |
import ( | |
"fmt" | |
) | |
func main(){ | |
var n int; | |
fmt.Scan(&n) | |
var a = make([]int,n) | |
var b = make([]int,n) | |
for i:=0;i<n;i++{ | |
fmt.Scan(&a[i]); | |
} | |
for i:=0;i<n;i++{ | |
fmt.Scan(&b[i]); | |
c:=a[i]+b[i] | |
fmt.Printf("%v",c) | |
fmt.Printf(" ") | |
} | |
} |
package main | |
import ( | |
"fmt" | |
) | |
func main(){ | |
var n int | |
fmt.Scan(&n) | |
var a = make([]int,n) | |
for i:=0;i<n;i++{ | |
fmt.Scan(&a[i]); | |
} | |
swap:=0 | |
for i:=0;i<n;i++{ | |
for j:=0;j<n-1;j++{ | |
if a[j]>a[j+1]{ | |
temp:=a[j] | |
a[j]=a[j+1] | |
a[j+1]=temp | |
swap++ | |
} | |
} | |
} | |
fmt.Println(swap) | |
} |
//https://www.hackerearth.com/practice/algorithms/string-algorithm/basics-of-string-manipulation/practice-problems/algorithm/monk-teaches-palindrome/ | |
package main | |
import ( | |
"fmt" | |
) | |
func reverse (b string) string{ | |
r := []rune(b) | |
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { | |
r[i], r[j] = r[j], r[i] | |
} | |
return string(r) | |
} | |
func isPalindrome(word string) bool { | |
if word == reverse(word) { | |
return true | |
} | |
return false | |
} | |
func checkEven(c int){ | |
if c%2==0 { | |
fmt.Println(" EVEN") | |
}else{ | |
fmt.Println(" ODD") | |
} | |
} | |
func main(){ | |
var n int | |
var a string | |
fmt.Scan(&n) | |
for i:=0;i<n;i++{ | |
fmt.Scan(&a) | |
if isPalindrome(a)==true { | |
fmt.Print("YES") | |
checkEven(len(a)) | |
}else{ | |
fmt.Println("NO") | |
} | |
} | |
} |
package main | |
import ( | |
"fmt" | |
) | |
func travel(m [20][20]int, l int) int { | |
var pairs int | |
for i := 0; i < l; i++ { | |
for p := i; p < l; p++ { | |
for j := 0; j < l; j++ { | |
for q := j; q < l; q++ { | |
if m[i][j] > m[p][q] { | |
pairs++ | |
} | |
} | |
} | |
} | |
} | |
return pairs | |
} | |
func main() { | |
var ( | |
t int | |
n int | |
m [20][20]int | |
) | |
fmt.Scanf("%d", &t) | |
for i := 0; i < t; i++ { | |
fmt.Scanf("%d", &n) | |
for p := 0; p < n; p++ { | |
for q := 0; q < n; q++ { | |
fmt.Scanf("%d", &m[p][q]) | |
} | |
} | |
fmt.Println(travel(m, n)) | |
} | |
} |
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func say(s string){ | |
for i:=0;i<5;i++{ | |
time.Sleep(100 * time.Millisecond) | |
fmt.Printf("%v : %d \n",s, i) | |
} | |
} | |
func main(){ | |
go say("Break") | |
say("dance") | |
} |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
var n int; | |
fmt.Scan(&n) | |
var a=make([]int,n) | |
for i:=0;i<n;i++ { | |
fmt.Scan(&a[i]) | |
} | |
for i:=0;i<n-1;i++ { | |
min:=a[i] | |
for j:=i+1;j<n;j++{ | |
if(a[j]<min){ | |
min=a[j] | |
a[j]=a[i] | |
a[i]=min; | |
} | |
} | |
} | |
fmt.Println(a) | |
} |
package main | |
import ("fmt") | |
func ternarySearch(l,n ,k int ,a[] int){ | |
mid1:=(l+(n-l))/3 | |
mid2:=(n-(n-l))/3 | |
if a[mid1]==k{ | |
fmt.Println(mid1) | |
}else if a[mid2]==k{ | |
fmt.Println(mid2) | |
}else if a[k]<a[mid1]{ | |
ternarySearch(l,mid1-1,k,a) | |
}else if a[k]>a[mid2]{ | |
ternarySearch(mid2+1,n,k,a) | |
}else{ | |
ternarySearch(mid1+1,mid2-1,k,a) | |
} | |
} | |
func main(){ | |
var n,k int | |
fmt.Scan(&n,&k) | |
var a=make([]int,n) | |
for i:=0;i<n;i++ { | |
fmt.Scan(&a[i]) | |
} | |
l:=0 | |
ternarySearch(l,n-1,k,a) | |
} |
package main | |
import ( | |
"fmt" | |
) | |
func main(){ | |
var i,l int | |
fmt.Println("Enter row colm of array") | |
fmt.Scan(&i) | |
m:=make([][]int,0,0) | |
for a:=0;a<i;a++{ | |
n:=make([]int,0,0) | |
for b:=0;b<i;b++{ | |
fmt.Scan(&l) | |
//n=append(n,l) | |
n=append(n,l) | |
} | |
m=append(m,n) | |
} | |
fmt.Println(m) | |
} |
package main | |
import ( | |
"fmt" | |
"strconv" | |
) | |
func main(){ | |
i, err := strconv.Atoi("42.5") | |
if err != nil { | |
fmt.Printf("couldn't convert number: %v\n", err) | |
return | |
} | |
fmt.Println("Converted integer:", i) | |
} |