JavaScripter (Noder|-) who tries to learn Golang (->one day may become a Gopher), let's see how it goes ...
NOTE : I am planning to make this article live i.e. will update something new from time to time , unless really something would require dedicated (separated) article tho ;
Every time I updated something new , I will give N+1 ed. in the title
IMPORTANT NOTE : Before begin, I understand we cannot compare weak dynamic (untyped) which is JavaScript with strong static (typed) which is Go , however in some places I may compare JS with Go as it's easy for me personally to learn by comparison whilst associating things in between ;
LEGEND : Go & Golang will be used interchangeably (mostly Golang) !
GLOSSARY :
Package – related source (hereinafter – src) files compiled (& linked) into one batch;
Module – inter-related packages ;
GLOBAL FUNCTIONS :
Omit the global prefix : window in JS, fmt in Go :
In JavaScript :
function main(){
/* window. */print()
}
main()
In Golang :
package main
import (
. "fmt"
)
func main(){
/* fmt. */Println("123")
}
ARRAYS
- UNTYPED ARRAYS
In JavaScript :
var nil_ = []; /* implicitly as type of int (use TS if you want to be sure) */
console.log(`${nil_.length == 0}\n`) // true
In Golang :
var nil_ []int
fmt.Println(len(nil_) == 0) // true
since 3.1 ed.:
-
TYPED ARRAYS
- declarations :
-
TYPED ARRAYS
- views :
In JavaScript :
/*var*/ primes = new ArrayBuffer(8)
/*var*/ uint8 = new Uint16Array(primes)
/*var*/ sliced_primes = uint8.slice()
console.log(sliced_primes) // Uint16Array(4) [0, 0, 0, 0, buffer: ArrayBuffer(8), byteLength: 8, byteOffset: 0, length: 4, Symbol(Symbol.toStringTag): 'Uint16Array']
In Golang :
func main() {
primes := [4]int{}
var sliced_primes []int = primes[0:len(primes)]
fmt.Println(sliced_primes)
}
// try oneself on : @https://go.dev/play/
BONUS for TypedArrays :
Let me share you my own draft you probably won't nowhere on www , unless some pricy book, again the explanation is state of draft & made upon working with JavaScript, but that's how compression / decompression (composition) works in high level :
NOTE : In JavaScript TypedArrays are limited of one specific type you choose to have in the container , whereas DataView (heterogenous data in fancy terms) can be used as a "translator" eliminating boundaries "of one type" for TypedArrays (homogenous data in fancy terms) .
TIP : with reference to explanation above , in Node.js runtime we would use
Buffer.from()
!
since 3.2 ed.:
- Slicing the arrays :
In JavaScript :
// Task : slice each season's months into dedicated variable & print it :
function main(){
var full_year = ["Jan", "Feb", "March", "April", "May", "June", "July","August", "September", "October", "November", "December",]
var winter = full_year.slice(0,2) winter.unshift(full_year[11])
var spring = full_year.slice(2,5)
var summer = full_year.slice(5,8)
var autumn = full_year.slice(8,11)
console.log(full_year, winter, spring, summer, autumn)
}
main()
In Golang :
package main
import (
. "fmt"
)
func main(){
full_year := []string{"Jan", "Feb", "March", "April", "May", "June", "July","August", "September", "October", "November", "December",}
/* --- */
// Task : slice each season's months into dedicated variable & print it :
var winter = append([]string{}, full_year[11], full_year[0], full_year[1])
var spring = full_year[2:5]
var summer = full_year[6:9]
var autumn = full_year[9:12]
Println(full_year, winter, spring, summer, autumn)
}
since 3.3 ed.:
[stay tuned for more...]
Discussion (1)
Upvoted for 3.2ed.