Skip to content

Instantly share code, notes, and snippets.

@rolroralra
Last active April 2, 2021 04:35
Show Gist options
  • Save rolroralra/d619e8e7ceb3306ec9a514035ea5fd9a to your computer and use it in GitHub Desktop.
Save rolroralra/d619e8e7ceb3306ec9a514035ea5fd9a to your computer and use it in GitHub Desktop.
Go Lang

go mod (proxy setting)

Details

## Windows10
$ set http_proxy=70.10.15.10:8080
$ set https_proxy=70.10.15.10:8080

# Linux
$ export http_proxy=70.10.15.10:8080
$ export https_proxy=70.10.15.10:8080

# Git Global Environment Variable
$ git config --global http.proxy 70.10.15.10:8080
$ git config --global https.proxy 70.10.15.10:8080
$ git config --global http.sslVerify false

# go mod refresh
$ go mod tidy


Framework


imports, variables, contants

Details

package main

import (
	"fmt"
	"math"
	"math/rand"
	"time"
)

const (
	Pi = math.Pi
	world = "world"

	// Create a huge number by shifting a 1 bit left 100 places.
	// In other words, the binary number that is 1 followed by 100 zeroes.
	Big = 1 << 100
	// Shift it right again 99 places, so we end up with 1<<1, or 2.
	Small
)

var (
	c, python, java bool = true, false, false
)

func main() {
	rand.Seed(time.Now().Unix())
	fmt.Printf("Now you have %.3f problems\n", math.Sqrt(7))
	
	var(
		i, j int = 1, 2
		z complex64 = 1 + 2i
		MaxInt int64 = (1 << 63) - 1
		MinInt int64 = -1 << 63
	)

	k := 3

	_ = k

	fmt.Println(i, j, c, python, java)
	fmt.Println(z)
	fmt.Println(MaxInt)
	fmt.Println(math.MinInt64)
	fmt.Println(MinInt)
	
	fmt.Println(Pi, world)
}


functions

Details

package main

import (
	"fmt"
	"math"
)
//func add(x int, y int) int {
func add(x, y int) int {
	return x + y
}

// multiple return
func swap(x, y string) (string, string) {
	return y, x
}

// naked return function
func split (sum int) (x int, y float64) {
	x = sum * sum
	y = math.Sqrt(float64(sum))
	return
}

func main() {
	fmt.Println(add(1, 2))

	a, b := swap("hello", "go")
	fmt.Println(a, b)

	fmt.Println(split(2))
}

if

Details

package main

import (
	"fmt"
	"math"
)

func pow(x, n, lim float64) float64 {
	if v := math.Pow(x, n); v < lim {
		return v
	} else {
		fmt.Printf("%g >= %g\n", v, lim)
	}
	// can't use v here, though
	return lim
}

func Sqrt(x float64) float64 {
	z := x / 2

	for i := 0; i < 10; i++ {
		z -= (z * z - x) / (2 * z)
	}

	return z
}

func main() {
	fmt.Println(
		pow(3, 2, 10),
		pow(3, 3, 20),
	)

	fmt.Println(Sqrt(2))
	fmt.Println(math.Sqrt(2))
}


for, while

package main

import "fmt"

func main() {
	// for
	sum := 0
	for i := 0; i < 10; i++ {
		sum += i
	}
	fmt.Println(sum)

	// for
	sum = 0
	for ; sum < 100; {
		sum += 1
	}
	fmt.Println(sum)

	// while
	sum = 0
	for sum < 20 {
		sum += 1
	}
	fmt.Println(sum)

	// while
	sum = 0
	for {
		if sum >= 50 {
			break
		}

		sum += 1
 	}
 	fmt.Println(sum)
}


switch

Details

package main

import (
	"fmt"
	"runtime"
	"time"
)

func main() {
	fmt.Println(runtime.GOOS)
	fmt.Print("Go runs on ")
	switch os := runtime.GOOS; os {
	case "darwin":
		fmt.Println("OS X.")
		//break
	case "linux":
		fmt.Println("Linux.")
		//break
	default:
		// freebsd, openbsd,
		// plan9, windows...
		fmt.Printf("%s.\n", os)
	}

	// variable in case
	fmt.Println("When's Saturday?")
	today := time.Now().Weekday()
	switch time.Saturday {
	case today + 0:
		fmt.Println("Today.")
	case today + 1:
		fmt.Println("Tomorrow.")
	case today + 2:
		fmt.Println("In two days.")
	default:
		fmt.Println("Too far away.")
	}

	// no condition switch
	t := time.Now()
	switch {
	case t.Hour() < 12:
		fmt.Println("Good morning!")
	case t.Hour() < 17:
		fmt.Println("Good afternoon.")
	default:
		fmt.Println("Good evening.")
	}
}


defer

Details

package main

import "fmt"

func main() {
	// defer work by STACK (LIFO)
	for i:=0; i< 10; i++ {
		defer fmt.Println("Bye...", i)
	}

	fmt.Println("Hello Go!")
}


How to print multiline string like python """

Details

package main

import "fmt"

func main() {
  fmt.Println(`
\    /\
 )  ( ')
(  /  )
 \(__)|
`)

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment