ico-arrow-big-left

GoLang Fun Challenge - Hello World

Key Information

Register
Submit
The challenge is finished.
Show Deadlines

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 

  1. Install go on your system from this link.
  2. Run the installer.

Step 2: Create Hello World app

  1. 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.
      2. Run the program.
  • 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.
Step 3:Take user input 
  1. Import bufio package in order implement buffered textual input output. 
  2. Import OS Package in order to read the input from console.
  3. Define a scanner to perform buffered textual input output operation from bufio package. 
  4. 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.
Step 4: Sort the entered string
  1. You need to write a function in order the sort the entered string. 
    1. You will need two package in order perform sorting. 1) strings  2) sort���
    2. Create the function of string type which should have 1 parameter(Value to be passed from main function).���
 //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 "".

    }
 

    3. Lets call the SortUsrIp in main function by assigning it to a variable.
    4. Print the value returned by the function SortUsrIp.
SortedText:=SortUsrIp(text)  
fmt.Print("\nYour sorted text is ->",SortedText)

 
     5.Your complete code would look like following. Execute the code with cmd go run HelloWorld.go
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 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
1. Define a simple function to display a string 5 times.
func LearnGR(TC string){
    for i := 0; i < 4; i++ {
       fmt.Println(TC, ":", i)}

}
2. Call the function LearnGR after the line in which you displayed input from the user.
Your code snippet would look like following.

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)
}
3. Execute the code. You would see following output because the funtion call LearnGR("topcoder") get executed first compeltely then the next line of code ge executed.
hello, world
Enter your text:ds
your entered text was:dstopcoder : 0
topcoder : 1
topcoder : 2
topcoder : 3
topcoder : 4
your sorted text is: ds
  4. Prefix the LearnGR("topcoder") with go in the main function.
.
..
...
fmt.Print("your entered text was:", text) //print the user input.
go LearnGR("topcoder")
SortedText:=SortUsrIp(text)

....
 5. Save the file and execute the code. You will see following output because goroutines works asyncronously. They initiates it's own thread and get executed independently while the rest of the program get executed. They get excuted in concurrence with the program. In our scenario the program was exited because eof was reached. Think of it in a scenario where you have to pull data from a webservice and while you waiting for the data to be fetched your system hangs...goroutine will save you from those situation and will execution time & memory efficient. 
hello, world
Enter your text:topcoder
your entered text was:topcoder
your sorted text is: cdeooprt

 
NOTE:- If you can't see above input then I recommend you to put some time lagging in LearnGR func. Following is the complete code with time lagging
 
package main
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.
 
Points to Ponder:-
  • 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
  1. We have learned how to import packages.
  2. We have learned how to create a function.
  3. We have learned how to read data from a user.
  4. We have learned how to sort the user data.
  5. 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. 

 

Reliability Rating and Bonus

For challenges that have a reliability bonus, the bonus depends on the reliability rating at the moment of registration for that project. A participant with no previous projects is considered to have no reliability rating, and therefore gets no bonus. Reliability bonus does not apply to Digital Run winnings. Since reliability rating is based on the past 15 projects, it can only have 15 discrete values.
Read more.

ELIGIBLE EVENTS:

2018 Topcoder(R) Open

REVIEW STYLE:

Final Review:

Community Review Board
?

Approval:

User Sign-Off
?

CHALLENGE LINKS:

Review Scorecard

?