Giter VIP home page Giter VIP logo

go-ipgen's Introduction

Simple Ip Address Generator from subnet

Build Status

Example Code

package main

import (
	"fmt"
	ipgen "github.com/wahyuhadi/go-ipgen"

)


func main(){

	ip := ipgen.IpAddressGen("10.30.0.0/16")

	for i:=0; i<len(ip); i++ {
		fmt.Println(ip[i])
	}

	fmt.Println(len(ip))
	// Do stuff with ip address

}
$ go run apps.go

Example Code with goroutine and mutex

package main

import (
	"context"
	"flag"
	"fmt"
	"sync"

	ipgen "github.com/wahyuhadi/go-ipgen"
)

var (
	workerCount = flag.Int("wk", 1, "Worker count")
	subnet      = flag.String("subnet", "192.168.1.1/24", "Subnet Network")
)

type Complete struct {
	N      int
	Lock   sync.Mutex
	Cancel context.CancelFunc
}

func main() {
	flag.Parse()

	ip := ipgen.IpAddressGen(*subnet)
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	var wg sync.WaitGroup

	ctr := &Complete{
		N:      len(ip),
		Cancel: cancel,
	}
	retChan := make(chan string, 4)
	for i := 0; i < *workerCount; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			work(ctx, retChan, ctr)
		}()
	}

	for _, i := range ip {
		retChan <- i
	}

	wg.Wait()
}

func work(ctx context.Context, retChan <-chan string, ctr *Complete) {
	for {
		select {
		case <-ctx.Done():
			return
		case ip := <-retChan:
			fmt.Println(ip)
			// Do stuff with ipaddress
			ctr.Lock.Lock()
			ctr.N--
			if ctr.N <= 0 {
				ctr.Lock.Unlock()
				ctr.Cancel()
				return
			}
			ctr.Lock.Unlock()
		}
	}
}
$ go run apps.go --wk=4 --subnet=192.168.1.1/16
  • --wk = worker
  • --subnet = network subnet

go-ipgen's People

Contributors

wahyuhadi 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.