Coder Social home page Coder Social logo

go-prime-generator's Introduction

go-prime-generator

Prime numbers generator on Golang with timeout functionality

install

go get github.com/Azorej/go-prime-generator

#how to

package main

import (
	"fmt"
	primes "github.com/Azorej/go-prime-generator"
	"time"
)

func main() {
	//generate primes less than 10000000, timeout 2 seconds
	primes := generate(10000000, time.Second*2)

	//primes count
	fmt.Println(len(primes))
}

func generate(maxN int, timeout time.Duration) []int {
	ch := make(chan int, 100)
	enough := primes.NewChanSignal()

	go primes.Generate(maxN, ch, enough)

	timer := time.NewTimer(timeout)
	res := make([]int, 0)

	for {
		select {
		case v, more := <-ch:
			if !more {
				return res
			}
			res = append(res, v)

		case <-timer.C:
			enough <- primes.NewSignal()
			println("Timeout :(")
			return res
		}
	}
}

go-prime-generator's People

Contributors

azorej avatar

Stargazers

Ulaş SAYGIN avatar Ahmad Dara avatar Andrey Kubarkov avatar

Watchers

James Cloos avatar  avatar

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.