You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
iferr!=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
iferr!=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.
typeArgumentsstruct {
DomainFilestring// first argument within the script
}
varArgsArguments// runs before mainfuncinit() {
iflen(os.Args) ==1 {
fmt.Println("Please supply a file")
os.Exit(0)
}
Args.DomainFile=os.Args[1]
}
funcmain() {
// other code here
}
Redundant Else statements
when you call to check an error or even check arguments as you did below
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.
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.
The text was updated successfully, but these errors were encountered:
Leaving open for now: Planning on refactoring and parsing better later once this project is close to completion, WIll implement cleaner/better error handling with next commit
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.
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.
would be replaced with
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
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 themain()
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.when you call to check an error or even check arguments as you did below
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.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.
The text was updated successfully, but these errors were encountered: