Skip to content

V for Go developers

JalonSolov edited this page Jan 18, 2023 · 11 revisions
Go V
Hello World
package main
import "fmt"
func main() {
  fmt.Println("Hello World!")
}
println('Hello World!')
Slice initialization
numbers := []int{1, 2, 3, 4}
numbers := [1, 2, 3, 4]
Add an element to a slice
numbers = append(numbers, 5)
numbers << 5
Printing a slice
fmt.Println(numbers)
println(numbers)
Filtering a slice
even := make([]int, 0)
for _, num := range numbers {
  if num % 2 == 0 {
    even = append(even, num)
  }
}
even := numbers.filter(it % 2 == 0)
Checking if a slice contains an element
contains := false
for _, num := range numbers {
  if num == x {
    contains = true
    break
  }
}
contains := x in numbers
Reading a file
import (
  "io/ioutil"
  "log"
)
b, err := ioutil.ReadFile(path)
if err != nil {
  log.Println(err)
  return
}
text := string(b)
import os
text := os.read_file(path) or {
  eprintln(err)
  return
}
Testing a function
package greeter_test
import (
  "testing"
)
func TestHello(t *testing.T) {
  if Hello() != "Hello" {
    t.Fatalf("Hello() failed")
  }
}
fn test_hello() {
  assert hello() == 'hello'
}