Go-lang was thought to be the final language for system applications.
One of the things most precious instruments (not) daily use for the administration of our system is certainly the park options and flags that we can command line.
Let's see, with a simple example, what Go provides us to make our application as flexible as possible.
/ / var Flags
file_to_download * string;
var string * fileType;
var configShow * string;
configEdit var bool *;
/ ** * Function used to initialize
flags from command line.
* /
initFlags func () {int
file_to_download flag.String = ("d ",""," Type the name of the file");
fileType = flag.String("t","*","Kind of file you want to download:\n\t*: all\n\tmov: movie\n\tmsc: music");
configShow = flag.String("c","editor","Open the config file into chosen [editor]");
configEdit = flag.Bool("C",false,"Edit parameters of config from inside the program."); }
Il package flag contiene una funzione di definizione parametro per ogni tipo di comando che desideriamo receive. All these functions but they all have the same shape. In fact receive three parameters:
The letter / letters that identify the command;
The default behavior;
A message to give to the operator on how the flag should be used.
Once we set the flags using the commands coming from the shell in our program:
/ ** * Main function . From here starts the program. * /
func main () {
- / / Initialize flags
- initFlags ();
- fileStr var string = "";
- confStr var string = ""; confMode var bool = false;
flag.Parse () / / Scan the list and arg sets up flags
typeStr var string = "";
confStr + = * configShow;
if (confMode) {
os.Stdout.WriteString ("Config mode \\ n");}
else {
os.Stdout.WriteString ("normal mode \\ n") ;}
if (* file_to_download == "") / / Want to download a file
{
fileStr + = "Error";
fileStr + = Newline;
os.Stdout.WriteString (fileStr)
} else {
fileStr + = * file_to_download;
fileStr + = Newline;
os.Stdout.WriteString (fileStr );}
typeStr + = * fileType;
typeStr + = Newline;
os.Stdout.WriteString (typeStr)
/ / Useless calls
giogolib.LogMessage (def.Debug, "Applying game");
}
I hope I have shown clearly how lang Go for providing the user who will use all our application the tools to adapt the software to run to their needs.
0 comments:
Post a Comment