Skip to content

Commit f530491

Browse files
author
Rajeev Kumar Singh
committed
Go Packages and Readme
1 parent 5cafd65 commit f530491

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

12-packages/app/main.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/callicoder/golang-tutorials/12-packages/numbers"
6+
"github.com/callicoder/golang-tutorials/12-packages/strings"
7+
str "strings"
8+
)
9+
10+
func main() {
11+
fmt.Println(strings.WelcomeText)
12+
fmt.Println(strings.Reverse("rajeev"))
13+
fmt.Println(numbers.IsPrime(9))
14+
fmt.Println(str.Contains("Hello, World", "Wo"))
15+
}

12-packages/numbers/prime.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package numbers
2+
3+
import "math"
4+
5+
// Checks if a number is prime or not
6+
func IsPrime(num int) bool {
7+
for i := 2; i <= int(math.Floor(math.Sqrt(float64(num)))); i++ {
8+
if num%i == 0 {
9+
return false
10+
}
11+
}
12+
13+
return num > 1
14+
}

12-packages/strings/constants.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package strings
2+
3+
var WelcomeText = "Hello, World"

12-packages/strings/reverse.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package strings
2+
3+
// Reverses a string
4+
/*
5+
Since strings in Go are immutable, we first convert the string to a mutable array of runes ([]rune),
6+
perform the reverse operation on that, and then re-cast to a string.
7+
*/
8+
func Reverse(s string) string {
9+
chars := []rune(s)
10+
for i, j := 0, len(chars)-1; i < j; i, j = i+1, j-1 {
11+
chars[i], chars[j] = chars[j], chars[i]
12+
}
13+
return string(chars)
14+
}

Readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Golang Tutorials
22

3+
0. [Golang Installation, Setup, GOPATH, and Go Workspace](https://www.callicoder.com/golang-installation-setup-gopath-workspace/)
4+
35
1. [Writing your first Go program](https://www.callicoder.com/golang-introduction-hello-world/)
46

57
2. [Golang Variables, Zero Values, and Type inference](https://www.callicoder.com/golang-variables-zero-values-type-inference/)

0 commit comments

Comments
 (0)