Programming Notes ✍️ – Telegram
Programming Notes ✍️
12 subscribers
65 photos
1 file
22 links
Download Telegram
np-manager
np-executer
Running One-Time Scripts: Use npx for executing packages that you don't want to install globally, like testing a new package or tool.

Quick Prototyping: Leverage npx to quickly test libraries and tools without cluttering your global installation with unused packages.
During the installation process, Yarn installs multiple packages at once as contrasted to npm that installs each one at a time. Reinstallation was also pretty fast when using Yarn. It's because of its offline mode feature that uses a caching mechanism to allow for fast download of previously downloaded packages.
struct pnpm

{
"name": "package-manager-playground",
"version": "1.0.0",
"packageManager": "pnpm@6.24.4",
"pnpm": {
"packageExtensions": {
"styled-components": {
"dependencies": {
"react-is": "*"
}
},
"autoprefixer": {
"dependencies": {
"postcss": "*"
}
}
}
},
// ...
}
To increase the capacity of a slice one must create a new, larger slice and copy the contents of the original slice into it. This technique is how dynamic array implementations from other languages work behind the scenes.
func copy(dst, src []T) int
The looping piece of this common operation is made easier by the built-in copy function. As the name suggests, copy copies data from a source slice to a destination slice. It returns the number of elements copied.
t := make([]byte, len(s), (cap(s)+1)*2)
copy(t, s)
s = t
A common operation is to append data to the end of a slice. This function appends byte elements to a slice of bytes, growing the slice if necessary, and returns the updated slice value:

func AppendByte(slice []byte, data ...byte) []byte {
m := len(slice)
n := m + len(data)
if n > cap(slice) { // if necessary, reallocate
// allocate double what's needed, for future growth.
newSlice := make([]byte, (n+1)*2)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:n]
copy(slice[m:n], data)
return slice
}
Go provides C-style /* */ block comments and C++-style // line comments. Line comments are the norm; block comments appear mostly as package comments, but are useful within an expression or to disable large swaths of code.
Go sorts packages based on the import relationships they form. When you import a package, it loads that package and everything it depends on first. That includes both code and init() functions. By the time Go starts to run your main package, it has already worked through every import it needed to get there. That means packages that live deeper in the graph run their init() functions earlier.
Declaration = ConstDecl | TypeDecl | VarDecl .
TopLevelDecl = Declaration | FunctionDecl | MethodDecl .
Go is lexically scoped using blocks:



The scope of a predeclared identifier is the universe block.
The scope of an identifier denoting a constant, type, variable, or function (but not method) declared at top level (outside any function) is the package block.
The scope of the package name of an imported package is the file block of the file containing the import declaration.
The scope of an identifier denoting a method receiver, function parameter, or result variable is the function body.
The scope of an identifier denoting a type parameter of a function or declared by a method receiver begins after the name of the function and ends at the end of the function body.
The scope of an identifier denoting a type parameter of a type begins after the name of the type and ends at the end of the TypeSpec.
The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.
The scope of a type identifier declared inside a function begins at the identifier in the TypeSpec and ends at the end of the innermost containing block.
Types:
any bool byte comparable
complex64 complex128 error float32 float64
int int8 int16 int32 int64 rune string
uint uint8 uint16 uint32 uint64 uintptr

Constants:
true false iota

Zero value:
nil

Functions:
append cap clear close complex copy delete imag len
make max min new panic print println real recover
Within a type parameter list of a generic type T, a type constraint may not (directly, or indirectly through the type parameter list of another generic type) refer to T.



type T1[P T1[P]] … // illegal: T1 refers to itself
type T2[P interface{ T2[int] }] … // illegal: T2 refers to itself
type T3[P interface{ m(T3[int])}] … // illegal: T3 refers to itself
type T4[P T5[P]] … // illegal: T4 refers to T5 and
type T5[P T4[P]] … // T5 refers to T4

type T6[P int] struct{ f *T6[P] } // ok: reference to T6 is not in type parameter list
The length of a slice may be changed as long as it still fits within the limits of the underlying array; just assign it to a slice of itself. The capacity of a slice, accessible by the built-in function cap, reports the maximum length the slice may assume. Here is a function to append data to a slice. If the data exceeds the capacity, the slice is reallocated. The resulting slice is returned. The function uses the fact that len and cap are legal when applied to the nil slice, and return 0.
slice > array[len]
if array[len] == len[arr] > new(arr[len(arr)+1] = "data") > copy to addr arr > replace
Programming Notes ✍️
slice > array[len] if array[len] == len[arr] > new(arr[len(arr)+1] = "data") > copy to addr arr > replace
func Append(slice, data []byte) []byte {
l := len(slice)
if l + len(data) > cap(slice) { // reallocate
// Allocate double what's needed, for future growth.
newSlice := make([]byte, (l+len(data))*2)
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:l+len(data)]
copy(slice[l:], data)
return slice
}