How to Use the Go Compiler
To use the Go Compiler, follow these steps:
- In the code editor, write your Go code.
- Click the "RUN" button to compile and run your code.
- The output will be displayed in the console below the code editor.
Taking Inputs
In Go, you can take inputs from the user in various ways. Here are some examples:
String Input
package main
import "fmt"
import "bufio"
import "os"
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter a string: ")
input, _ := reader.ReadString('\n')
fmt.Println("You entered: ", input)
}
Number Input
package main
import "fmt"
func main() {
var num int
fmt.Print("Enter a number: ")
fmt.Scanf("%d", &num)
fmt.Println("You entered: ", num)
}
Importing Libraries
Go has a rich set of built-in libraries that can be used in your programs. Here are some examples:
Using the math Library
package main
import "fmt"
import "math"
func main() {
result := math.Sqrt(25)
fmt.Println("Square root of 25 is ", result)
}
Using the sort Library
package main
import "fmt"
import "sort"
func main() {
arr := []int{5, 2, 8, 7, 1}
sort.Ints(arr)
fmt.Println("Sorted array: ", arr)
}
Syntax 101
Go is a statically typed, compiled language designed for simplicity and efficiency. Here's a primer on the major syntax basics of Go:
Variables
Variables in Go are statically typed and can be declared in different ways.
var name string = "John Doe" // Variable declaration with type and initial value
var age = 25 // Variable declaration with initial value, type inferred
Control Structures
Go includes control structures such as if
, else
, for
, and switch
.
// If-Else
if age > 18 {
fmt.Println("Adult")
} else {
fmt.Println("Minor")
}
// For loop
for i := 0; i < 5; i++ {
fmt.Println(i)
}
// Switch
switch day {
case "Monday":
fmt.Println("Start of the work week")
case "Friday":
fmt.Println("End of the work week")
default:
fmt.Println("Midweek")
}
Functions
Functions in Go are defined with the func
keyword.
func greet(name string) {
fmt.Println("Hello, " + name)
}
greet("John Doe") // Calls the function with "John Doe" as the argument
Go Online Test
A Go online test is an effective way to assess an individual's Go programming skills, especially in the context of systems programming. These tests typically include a mix of theoretical questions and practical coding challenges. By attempting these tests, candidates can demonstrate their understanding of Go concepts, their problem-solving abilities, and their proficiency in writing efficient code. Go online tests are commonly used in technical interviews, coding bootcamps, and online learning platforms to gauge a learner's understanding and proficiency in Go.