Coder Social home page Coder Social logo

thinkerou / 30-seconds-of-go Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 1.0 17 KB

Curated collection of useful Go snippets you can understand in 30 seconds or less.

Home Page: https://thinkerou.github.io/30-seconds-of-go/

License: MIT License

Makefile 56.02% Shell 18.09% Go 25.89%
go 30-seconds-of-code snippets learning-resources 30-seconds-of-go-code

30-seconds-of-go's Introduction

30 seconds of Go

Build Status codecov Go Report Card GoDoc

Curated collection of useful Go snippets that you can understand in 30 seconds or less.

Note: The project is inspired by 30 seconds of code, but there is no affiliation with that project.

Installation

โš ๏ธ NOTICE: A few of our snippets are not yet optimized for production (see disclaimers for individual snippet issues).

To install this package, you need to install Go and setup your Go workspace on your computer. The simplest way to install the library is to run:

$ go get -u github.com/thinkerou/30-seconds-of-go

Prerequisites

This requires Go 1.12 or later.

Table of Contents

๐Ÿ”Œ Adapter

View contents

๐Ÿ“š Array

View contents

โฑ๏ธ Date

View contents

๐ŸŽ›๏ธ Function

View contents

โž— Math

View contents

๐Ÿ—ƒ๏ธ Object

View contents

๐Ÿ“œ String

View contents

๐Ÿ“ƒ Type

View contents

๐Ÿ”ง Utility

View contents

โž— Math

average

Returns the average of two or more numbers.

Examples


โฌ† Back to top

averageBy

Returns the average of an array, after mapping each element to a value using the provided function.

Examples


โฌ† Back to top

fibonacci

Geerates an array, containing the Fibonacci sequence, up until the nth term.

Examples


โฌ† Back to top

gcd

Calculates the greatest common divisor between two or more numbers/arrays.

Examples


โฌ† Back to top

isEven

Returns true if the given number is even, false otherwise.

Checks whether a number is odd or even using the modulo (%) operator or and (&) operator. Returns true if the number is even, false if the number is odd.

func isEven(i int) bool {
	return i % 2 == 0
}

func isEven(i int) bool {
	return i & 1 == 0
}
Examples
isEven(-1) // false
isEven(-2) // true
isEven(3) // false
isEven(4) // true


โฌ† Back to top

isPowerOf2

Returns true is the given positive integer is the power of 2, false otherwise.

Checks whether a positive integer is the power of 2 using the and (&) operator.

func is PowerOf2(i uint) bool {
	return i & (i -1) == 0
}
Examples
isPowerOf2(1) // true
isPowerOf2(2) // true
isPowerOf2(3) // false
isPowerOf2(4) // true


โฌ† Back to top

isPrime

Checks if the provided integer is a prime number.

Check numbers from 2 to the square root of the given number. Return false if any of them divides the given number, else return true, unless the number is less than 2.

func isPrime(n int) bool {
	boundary := int (math.Floor(math.Sqrt(float64 (n))))
	for i := 2;  i <= boundary; i++ {
		if n % i == 0 {
			return false
		}
	}
	return n >= 2
}
Examples
isPrime(0) // false
isPrime(1) // false
isPrime(2) // true
isPrime(3) // true
isPrime(4) // false
isPrime(11) // true


โฌ† Back to top

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.