0% found this document useful (0 votes)
71 views

Javascript To Go Cheat Sheet

This cheat sheet provides a summary of common Javascript concepts and their equivalents in the Go programming language. Key differences include: - Go has type safety and does not support variable type coercion. Variables must be declared with an explicit type. - Go does not have block scope for variables declared with the var keyword - these variables are accessible from anywhere in the same package file. - Go uses structs instead of classes but supports methods on structs similarly to prototype-based inheritance in Javascript. - Go has first-class support for slices and maps instead of arrays and objects. Functions can accept variable arguments. - Go has first-class support for concurrency with goroutines instead of callbacks

Uploaded by

Admin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Javascript To Go Cheat Sheet

This cheat sheet provides a summary of common Javascript concepts and their equivalents in the Go programming language. Key differences include: - Go has type safety and does not support variable type coercion. Variables must be declared with an explicit type. - Go does not have block scope for variables declared with the var keyword - these variables are accessible from anywhere in the same package file. - Go uses structs instead of classes but supports methods on structs similarly to prototype-based inheritance in Javascript. - Go has first-class support for slices and maps instead of arrays and objects. Functions can accept variable arguments. - Go has first-class support for concurrency with goroutines instead of callbacks

Uploaded by

Admin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Javascript to Go Cheat Sheet

Javascript Go

somefunction() func main() {


somefunction()
}

var myFunc = function() {}; var myFunc = func(){}

function myFunc() { func myFunc() {

} }

var x = 5; var x int = 5 (anywhere)


x := 5 (only in func)

var x = 5; // can’t do this


x = “Hello”;

// no constants const x = 5

var x = 1, y = 2; var x, y = 1, 2

var x; // undefined var x int // 0

“Hello World” var mySaying string = “Hello”


var myBacktick string = `He“ll”o`

1234 1234

1234.2 1234.2
+, -, /, % +, -, /, %

true, false true, false


&& &&
|| ||

x === y x == y

if (i < 10) if i < 10 {


{ } else if (i < 20) {
} else {
}
else if (i < 20) }
{

}
else
{

while (whatever) { for whatever {

} }

while (true) { for {

} }

for (var i = 0; i < 10; i++) { for i := 0; i < 10; i++ {

} }
var i; var i int
for (i = 0; i < 10; i++) { for i = 0; i < 10; i++ {

} }

var obj = { obj := map[string]string {


“x”: “y”, "x": "y",
“y”: 10, "y": "z",
}; }
for (var key in obj) { for key := range obj {
console.log(“Key is:”, key); fmt.Println(“Key is:”, key)
console.log(“Value is:”, obj[key]); fmt.Println(“Value is:”, obj[key])
} }

var xs = [1,2,3,4]; xs := [4]int {1,2,3,4}

var xs = [1,2,3,4]; xs := []int {1,2,3,4}


xs.push(5,6,7,8); xs = append(xs,5,6,7,8)

// add to head import “fmt”


<script src=”fmt.js”></script>
// fmt.js // fmt.go
fmt = { func Println() {
Println: function() { }
} }
// or
var fmt = require(“fmt”)

function sum() { func sum(xs ...int) int {


for (var i=0; i<arguments.length; for key, value := range xs {
i++) {
}
} } sum(1,2,3) sum([]{1,2,3}
}

(function(n) { var factorial func(int) int


if (n == 0 || n == 1) { factorial = func(n int) int {
return 1; if n == 0 || n == 1 {
} else { return 1
return n * } else {
arguments.callee(n-1); return n * factorial(n-1)
} }
})(5) }

function MyClass(x) { type MyClass struct {


this.x = x; x int
} }
MyClass.prototype = { func NewMyClass(x int) *MyClass {
“whatever”: function() { return &MyClass{
console.log(this.x); x: x,
} }
}; }
MyClass.prototype.someOtherMethod = func (this *MyClass) whatever() {
function() { fmt.Println(this.x)
} bs, err := ioutil.ReadAll(f)
}; if err != nil {
var obj = new MyClass(5); log.Fatalln("my program broke")
obj.whatever(); }

str := string(bs)
func main() {
obj := NewMyClass(5)
obj.whatever()
}
var str = JSON.stringify({ “a”: “b”}) bs, err : = json.Marshal(map[string]string{“a”:”b”})

try { var obj map[string]string


var obj = JSON.parse(str) err := json.Unmarshal(str, &obj)
} catch(err) {

You might also like