Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/vikrammel/golang-ipsorting

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/vikrammel/golang-ipsorting

  • v0.0.0-20180106193839-69a4bb33fac7
  • Source
  • Go
  • Socket score

Version published
Created
Source

Sorting IP:Ports in Golang

by Vikram Melkote

Setup

  • git clone into ~/go/src or download .go files and place in ~/go/src/ipsorting:

    git clone https://github.com/Vikrammel/golang-ipsorting.git

  • if vendoring, place .go files in <projectroot>/app/vendor/ipsorting

  • import package in code:

    import (
    	"ipsorting"
    )
    

Usage in code

use on string or an array/slice of strings of ip:ports (ipsorting.go can be easily modified to work with just IPs instead of IP:Port)

  • compare two IP:Ports

    sortedPair := ipsorting.OrderIPPair("10.0.0.14:8080", "10.0.0.14:5060")
    //sortedPair is now a string array literal of size 2 of the two ip:port strings passed in
    //in ascending numerical order
    log.Println(sortedPair[0])
    //10.0.0.14:5060
    
  • sort a slice of IP:Ports

    //unsortedList = ["10.0.0.24:8080", "10.0.0.21:8080", "10.0.0.22:8080", "10.0.0.23:8080"]
    sortedList := ipsorting.SortIPs(unsortedList)
    log.Println(sortedList)
    //["10.0.0.21:8080", "10.0.0.22:8080", "10.0.0.23:8080", "10.0.0.24:8080"]
    //ports do not need to be the same, they will also be compared
    //adapted quickSort, avg complexity = O(nlog(n)), worst case = O(n^2)
    
  • insert IP:Port into slice in correct position

    //sortedList = ["10.0.0.21:8080", "10.0.0.22:8080", "10.0.0.23:8080", "10.0.0.24:8080"]
    sortedList = ipsorting.InsortIP(sortedList, "10.0.0.23:8070")
    log.Println(sortedList)
    //["10.0.0.21:8080", "10.0.0.22:8080", "10.0.0.23:8070", "10.0.0.23:8080", "10.0.0.24:8080"]
    //adapted binarySearch, complexity = O(log(n))
    

Reference

Quicksort in python

Binary Search [for InsortIP()]

Feel free to use this in any of your projects and please let me know if you encounter any bugs. Thank you.

FAQs

Package last updated on 06 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc