Skip to content

Instantly share code, notes, and snippets.

@manjeettahkur
manjeettahkur / forloop.go
Created May 30, 2022 06:37
Difference between for loop vs for range loop
package main
import (
"fmt"
)
func main() {
s := []int{1, 2}
for i := range s {
fmt.Println(i)
@manjeettahkur
manjeettahkur / LambdaConstants.java
Created May 25, 2022 15:53 — forked from seeebiii/LambdaConstants.java
Available default environment variables in AWS Lambda. Just copy&paste into your Node or Java project.
public class Constants {
/**
* Contains the path to your Lambda function code.
*/
public static final String LAMBDA_TASK_ROOT = System.getenv("LAMBDA_TASK_ROOT");
/**
* The environment variable is set to one of the following options, depending on the runtime of the Lambda function:
* AWS_Lambda_nodejs, AWS_Lambda_nodejs4.3, AWS_Lambda_nodejs6.10
@manjeettahkur
manjeettahkur / golang-print-char.go
Created January 29, 2022 08:25
verbs for print character in golang
package main
import "fmt"
func main() {
s := "Hello world!"
fmt.Printf("%c\n", s[0]) // the character represented by the corresponding Unicode code point
fmt.Printf("%q\n", s[0]) // a single-quoted character (rune) at index 0
fmt.Printf("%U\n", s[0]) // print Unicode code point at index 0
fmt.Printf("%b\n", s[0]) // print binary representation of Unicode code point at index 0
@manjeettahkur
manjeettahkur / jest.js
Last active May 25, 2022 13:09
mock mongoose methods with jest
// Example how to mock mongoose method internal method
// jest | mongoose
jest.fn().mockImplementationOnce(() => ({
toObject: jest.fn().mockResolvedValue({id: "2333"}),
lean: jest.fn().mockResolvedValue(null),
}))
// 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 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.
// An identifier declared in a block may be redeclared in an inner block. While the identifier of the inner declaration is in scope, i
@manjeettahkur
manjeettahkur / golang-for-loop.go
Created January 24, 2022 11:40
Why this code return this {5,5,5,5,5} ?
package main
import ("fmt")
func main() {
first := []string{"1","2","3","4","5"};
second := []*string{}
for _, item := range first {
second = append(second, &item)
}
package main
import (
"log"
"net/http"
"time"
)
func main() {
log.Println("Starting server on localhost:9090")
// Rune vs Byte
package main
import "fmt"
func main() {
const placeOfInterest = `⌘`
fmt.Printf("% x\n", placeOfInterest)
//range gives Unicode codepoints and the cost of parsing the next UTF-8 sequence is a reasonable, predictable, cost.
for _, v := range placeOfInterest {
@manjeettahkur
manjeettahkur / vscode-go-setting.json
Last active January 26, 2022 16:09
Go Language tools setting inside vs-code. it will check all the rules using go tools and show warning or error if any found. for example Structure size optimization in Go-lang (alignment/padding). setting for this "fieldalignment": true,
{
"editor.fontFamily": "Fira Code",
"editor.fontLigatures": true,
"editor.fontWeight": "500",
"editor.suggestSelection": "first",
"editor.cursorBlinking": "expand",
"editor.cursorSmoothCaretAnimation": true,
"terminal.integrated.rightClickBehavior": "default",
"editor.fontSize": 16,
"workbench.colorCustomizations": {

Manily two types are there

named types unnamed type

A simple way of thinking about it is that named types are those you define with the type statement, and unnamed types are composite types defined by a type literal.

An unnamed type, is just a type declared without a name by using a type literal, i

Print Verbs

The %#v format verb is shorthand to see both type and value,