Skip to content

Instantly share code, notes, and snippets.

@vovanmix
Last active May 19, 2017 01:15
Show Gist options
  • Save vovanmix/eb962fa8e1bd4f4586737f7627f3b31f to your computer and use it in GitHub Desktop.
Save vovanmix/eb962fa8e1bd4f4586737f7627f3b31f to your computer and use it in GitHub Desktop.
Languages cheatcheet

Go

for index, runeValue := range nihongo {
  fmt.Printf("%#U starts at byte position %d\n", runeValue, index)
}

Go

//  To test for a key without retrieving the value, use an underscore in place of the first value: 
_, ok := m["route"]

Go

import "math"

math.MaxInt32

Go

//Or however many you might need + buffer.
c := make(chan int, 300)
//Push
c <- value
//Pop
x <- c
// Pop in a loop
for {
	select {
	case point := <-c:
		return point
	default:
		return "none left"
	}
}

Standard Hackerrank pattern:

The first line contains an integer, N, denoting the size of the array.

The second line contains N space-separated integers representing the array's elements.

Go

Using scan:

func read() (int, int, []int) {
    var a, b, c int
    fmt.Scanf("%d %d %d", &a, &b, &c)
    
    arr := make([]int, a)
    for i := 0; i < a; i++ {
        fmt.Scanf("%d", &arr[i])
    }
    
    return b, c, arr
}

Simple:

import "fmt"

func readNum() int {
    var input int
    fmt.Scanln(&input)
    return input
}
import (
    "fmt"
    "strings"
    "bufio"
    "os"
    "strconv"
    "regexp"
)

var reader = bufio.NewReader(os.Stdin)

func readArray() []int {
    numbers, _ := reader.ReadString('\n')  
    numbers = regexp.MustCompile(`\r?\n`).ReplaceAllString(numbers, "")
    ar := strings.Split(numbers, " ")
    var int_ar = make([]int, len(ar))
    for i, el := range ar {
        int_ar[i], _ = strconv.Atoi(el)
    }
    return int_ar
}

func readNum() int {
    str, _ := reader.ReadString('\n')
    str = regexp.MustCompile(`\r?\n`).ReplaceAllString(str, "")
    num, _ := strconv.Atoi(str)
    return num
}

Scala

object Solution {
  def main(args: Array[String]) {
    val _ = scala.io.StdIn.readInt()
    val input = scala.io.StdIn.readLine()
    val arr: Array[Int] = input.split(" ").map(_.toInt)
  }
}


io.Source.stdin.getLines().take(2).map(_.toInt)

Go

import "fmt"

sum := 10
fmt.Println(sum)

Scala

val sum = 100
println(sum)

Go

switch os := runtime.GOOS; os {
	case "darwin":
		fmt.Println("OS X.")
	case "linux":
		fmt.Println("Linux.")
	default:
		// freebsd, openbsd,
		// plan9, windows...
		fmt.Printf("%s.", os)
	}

Go

type Pair struct {
    index, steps interface{}
}

a := Pair{newInd, steps + 1}

// or
a := struct {string; int}{"http:...", 3}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment