Skip to content

V for Go developers

Alexander Medvednikov edited this page Nov 25, 2019 · 11 revisions
Hello World
package main
import "fmt"
func main() {
  fmt.Println("Hello World!")
}
fn main() {
  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 vector
even := make([]int, 0)
for _, num := range numbers {
  if num % 2 == 0 {
    even = append(even, num)
  }
}
even := numbers.filter(it % 2 == 0)
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'
}