Challenge Overview
Abstract
We are launching this fun challenge to introduce you to GO programming language.
Important Note:
This is a fun challenge. No prize money will be awarded for completing this challenge successfully.
Challenge Details
What is Go?
Go is an open-source programming language majorly used in server side applications because of it's concurrency model.
Requirements for this challenge
In this challenge you would be writing code in notepad and compiling it with terminal/cmd. This challenge pervails to give you a head start on go by covering basics of go. You would learn how to build a hello world app, use go routines and it's package to perform certain operations.
Step 1: Setup Dev environment
- Install go on your system from this link.
- Run the installer.
Step 2: Create Hello World app
- Open notepad and type in following code
package main
import ("fmt" )
func main(){
fmt.Printf("hello, world\n")
}
Let's understand each line of code in above snippet.
- package main - Package will indicate compiler that "main" is the entry point of the executable program.
- import("fmt") - Import keyword will indicate compiler to include all the behaviourial aspect of package fmt within our program. fmt is a package which is responsible for input output operation in Go programming language.
- func main() - Entry point of our executable program which will be called by the system the moment we execute our program.
- fmt.Printf("") - Printf is one amongst many module within fmt package which responsible for displaying output/value of a variable.
- Save the file with .go extension. Once you save the file your file name should look like HelloWorld.go
- Open command prompt/terminal and jump to the folder where you saved the HelloWorld.go file and run following command
- go run HelloWorld.go
- You would see and output hello world on your command prompt/terminal.
- Import bufio package in order implement buffered textual input output.
- Import OS Package in order to read the input from console.
- Define a scanner to perform buffered textual input output operation from bufio package.
- Define a variable of string type to store user input on cmd/terminal.Your code will look like following
package main
import ("fmt"
"bufio"
"os")
func main(){
fmt.Printf("hello, world\n")
scanner := bufio.NewScanner(os.Stdin)// this will enable us to read user input from console
var text string // Variable to store value entered by user
fmt.Print("Enter your text:")
scanner.Scan() //Scan is one of the function amongst many in bufio package which will read every key press entered by user and save it in buffer
text = scanner.Text() //Assigns user input to text variable which we had defined above.
fmt.Print("Your entered text was:", text) //prints the user input.
}
5. Execute your code.
- You will be prompted to input something.
- On enter key press you would see what you have entered the last line of the code will be executed.
- You need to write a function in order the sort the entered string.
- You will need two package in order perform sorting. 1) strings 2) sort���
- Create the function of string type which should have 1 parameter(Value to be passed from main function).���
func SortUsrIp(sortIp string) string {
s := strings.Split(sortIp, "") //Split is a function which will slice the string. In our scenario(let's assume topcoder was user input) s would be["t" "o" "p" "c" "o" "d" "e" "r"] because our seperator was "". Had it been "c" then the split would have made 2 slices out of topcoder that's ["top" "oder"].
sort.Strings(s)//Sort function sorts the slice of the strings in increasing order. That's the reason we sliced the input using split function above.
return strings.Join(s, "")// Join function will join up all the slice made with "".
}
4. Print the value returned by the function SortUsrIp.
fmt.Print("\nYour sorted text is ->",SortedText)
import ("fmt"
"bufio"
"os"
"strings"
"sort")
//Function which will anticipate one value on calling from main function and would return a string back to the main function.
func SortUsrIp(sortIp string) string {
s := strings.Split(sortIp, "") //Split is a function which will slice the string. In our scenario(let's assume topcoder was user input) s would be["t" "o" "p" "c" "o" "d" "e" "r"] because our seperator was "". Had it been "c" then the split would have made 2 slices out of topcoder that's ["top" "oder"].
sort.Strings(s)//Sort function sorts the slice of the strings in increasing order. That's the reason we sliced the input using split function above.
return strings.Join(s, "")// Join function will join up all the slice made with "".
}
func main(){
fmt.Printf("hello, world\n")
scanner := bufio.NewScanner(os.Stdin)//this will enable us to read user input from console
var text string //Variable to store value entered by user
fmt.Print("Enter your text:")
scanner.Scan() //Scan is one of the function amongst many in bufio package
text = scanner.Text() //Assign user input to text variable which we had defined above.
fmt.Print("your entered text was:", text) //print the user input.
SortedText:=SortUsrIp(text)
fmt.Print("\nYour sorted text is:", SortedText)//printed sorted string
}
Step 5: Goroutine
for i := 0; i < 4; i++ {
fmt.Println(TC, ":", i)}
}
package main
import ("fmt"
"bufio"
"os"
"strings"
"sort"
)
//Function which will anticipate one value on calling from main function and would return a string back to the main function.
func SortUsrIp(sortIp string) string {
s := strings.Split(sortIp, "") //Split is a function which will slice the string. In our scenario(let's assume topcoder was user input) s would be["t" "o" "p" "c" "o" "d" "e" "r"] because our seperator was "". Had it been "c" then the split would have made 2 slices out of topcoder that's ["top" "oder"].
sort.Strings(s)//Sort function sorts the slice of the strings in increasing order. That's the reason we sliced the input using split function above.
return strings.Join(s, "")// Join function will join up all the slice made with "".
}
func LearnGR(TC string){
for i := 0; i < 5; i++ {
fmt.Println(TC, ":", i)
}
}
func main(){
fmt.Printf("hello, world\n")
scanner := bufio.NewScanner(os.Stdin)//this will enable us to read user input from console
var text string //Variable to store value entered by user
fmt.Print("Enter your text:")
scanner.Scan() //Scan is one of the function amongst many in bufio package
text = scanner.Text() //Assign user input to text variable which we had defined above.
fmt.Print("your entered text was:", text) //print the user input.
LearnGR("topcoder")
SortedText:=SortUsrIp(text)
fmt.Println("\nyour sorted text is:", SortedText)
}
Enter your text:ds
your entered text was:dstopcoder : 0
topcoder : 1
topcoder : 2
topcoder : 3
topcoder : 4
your sorted text is: ds
..
...
fmt.Print("your entered text was:", text) //print the user input.
go LearnGR("topcoder")
SortedText:=SortUsrIp(text)
....
Enter your text:topcoder
your entered text was:topcoder
your sorted text is: cdeooprt
import ("fmt"
"bufio"
"os"
"strings"
"sort"
"time")
//Function which will anticipate one value on calling from main function and would return a string back to the main function.
func SortUsrIp(sortIp string) string {
s := strings.Split(sortIp, "") //Split is a function which will slice the string. In our scenario(let's assume topcoder was user input) s would be["t" "o" "p" "c" "o" "d" "e" "r"] because our seperator was "". Had it been "c" then the split would have made 2 slices out of topcoder that's ["top" "oder"].
sort.Strings(s)//Sort function sorts the slice of the strings in increasing order. That's the reason we sliced the input using split function above.
return strings.Join(s, "")// Join function will join up all the slice made with "".
}
func LearnGR(TC string){
for i := 0; i < 5; i++ {
fmt.Println(TC, ":", i)
time.Sleep(1*time.Second)
}
}
func main(){
fmt.Printf("hello, world\n")
scanner := bufio.NewScanner(os.Stdin)//this will enable us to read user input from console
var text string //Variable to store value entered by user
fmt.Print("Enter your text:")
scanner.Scan() //Scan is one of the function amongst many in bufio package
text = scanner.Text() //Assign user input to text variable which we had defined above.
fmt.Print("your entered text was:", text) //print the user input.
//go LearnGR("topcoder")
LearnGR("topcoder")
SortedText:=SortUsrIp(text)
fmt.Println("\nyour sorted text is:", SortedText)
}
With this I will bring this challenge to an end.
- Make sure you always keep packages initials capital else it will throw an error. Ex:- fmt.Print is correct but fmt.print will produce an error.
- Import a package only if you use it else compiler will throw an error.
Let's Recap
- We have learned how to import packages.
- We have learned how to create a function.
- We have learned how to read data from a user.
- We have learned how to sort the user data.
- We have learned why to use goroutines
Happy Learning and Coding!
Final Submission Guidelines
Important Note:This is a fun challenge. No prize money will be awarded for completing this challenge successfully.
Submit your HelloWorld.go with screenshot of your output in terminal or command prompt window compressed in a folder.