Coder Social home page Coder Social logo

lazyrecon's People

Contributors

wyvdoesdev avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

lazyrecon's Issues

Issues - Oh so many issues

Golang is a pretty unique and also very flexible language, as I have had quite a round of experience with the language, and reading your message in the discord server, I figured I would take a look at your code. That being said, I have a unique sense of how handling this should have been done with a more- unique sense of code

> Error Handling

I noticed that in your code you either print or completely log the error as fatal under each error. Typically, if you are going to do this in a more direct production environment where errors are not handled, then you may want to create a function to handle the errors. For example, take the function below that would cut your code down by 20+ lines.

// error handling function
func CheckError(x error, message string) {
    if x != nil {
        log.fatal(message, x)
    }
}

you would call these functions where bricks of conditions are met not only to make the code much more organized but in some states less cluttered.

if err != nil {
			log.Fatalf("failed to open domains file: %v", err)
		}

would be replaced with

CheckError(err, "failed to open domains file:")

and you get the idea. I also notice that you use the log function to call this over and over within each conditional, it might be suggestive to just use another function to de-clutter. Some people do enjoy just using

				if err != nil {
					log.Print(err)
				}

but sometimes depending on the case you may want to automate that function.

> Using initiation functions

an overlooked thing within the Go programming language is the use of init functions. Init functions are initiation functions that are run before the main() function is. They can not be called, can not take arguments, and can not return data. The purpose? Well, when setting up arguments especially positional arguments within a program, it's important to maybe store data within a structure and then use init functions to fill the data. For example, take the code below.

type Arguments struct {
            DomainFile string // first argument within the script
}
var Args Arguments

// runs before main
func init() {
    	if len(os.Args) == 1 {
		fmt.Println("Please supply a file")
		os.Exit(0)
	} 
        Args.DomainFile = os.Args[1]  
}

func main() {
     // other code here
}

Redundant Else statements

when you call to check an error or even check arguments as you did below

	if len(os.Args) == 1 {
		fmt.Println("Please supply a file")
		os.Exit(0)
	} else {
        ...
} 

you create a redundant else statement. If the length of the operating system positional arguments is equal to 1 then you exit, but the program's native structure does not require an else since the code already exits if the condition is true and if the condition is false will just continue as normal. So, the else statement is not so needed here.

> Conditional Checking And Data Verification

Another huge missed thing with Go is the factor that some clients will auto-fill data, it's important that when parsing files and unique logic you make sure the data being supplied is correct especially when parsing them into http links. The following code shows what you have done.

			text := fileScanner.Text()
			fmt.Println(text)
			resp, err := http.DefaultClient.Get(fmt.Sprintf("http://%s", text))

when scanning the file and calling the default client, it might be worth it to not fully trust the user or even a program to properly supply direct host names. In the event that this is a bigger framework for future notice, you can use regular expressions or even string operations since Go has amazing support for that to check and verify that it is a properly formatted host for said needs.

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.