rodriggj / Go

0 stars 0 forks source link

1.7 Learn the basics of os.Args #7

Open rodriggj opened 2 years ago

rodriggj commented 2 years ago

View basics of os.Args

  1. Create a main.go file and input the following code:
    
    package main

import ( "fmt" "os" )

func main() { fmt.Printf("%#v\n", os.Args) }


> **NOTE:** We are using `fmt.Printf` vs `fmt.Println`. 

2. Now run the file and view the output in the terminal 
```go 
go run main.go

Results in ...

  1. Here you can see in the output that there is a slice of strings. The first arg in the slice points to the temporary path of the running program.

NOTE: The go run ... command only creates a temporary version of the running program. If you want to create a permanent location of the running program on your host you would need to run go build ... at the command line.

rodriggj commented 2 years ago

Using the go build ... command

  1. If you want to use a persistent version of the running program you need to use the go build ... vs. the go run... command.

  2. Run the following command with our main file and see the difference.

    go build -o greeter

ERROR: In the lesson, the instructor was able to just run this command, but when I tried I had to first run go mod init example.com/go-demo-2 which created a go.mod file. Then I was able to run go build -o greeter which created a greeter file without the go extension. I was then able to run ./greeter and see the contents of the Args[] slice. The contents showed what was presented in the lesson so it ultimately worked, just needed the additional steps.

  1. Now that there is a permanent file created with Args[] you can add variables to the slice in positions Args[1], Args[2], etc. To do this at the command line write the following code.
    ./greeter "hey" "ho" "howdy"

Results in ...

image

rodriggj commented 2 years ago

Now modify your Script

  1. Now that we see what the args slice is doing and where the file path is stored. Lets modify the main.go file to reference the Args slice and print the contents of the slice using fmt.Println() function. Modify the main.go script as follows:
    
    package main

import ( "fmt" "os" )

func main() { fmt.Printf("%#v\n", os.Args)

fmt.Println("Path:", os.Args[0])
fmt.Println("1st Argument:", os.Args[1])
fmt.Println("2nd Argument:", os.Args[2])
fmt.Println("3rd Argument:", os.Args[3])

fmt.Println("Number of items inside os.Args:", len(os.Args))

}


> **NOTE:** The last line of this example use the `len()` function which takes a data structure like a slice and returns the quantity of items within the data structure you pass in. In this case it will return the "length" of the os.Args slice. 

2. To run this program you have to re-build the program so run the following:
```go 
go build -o greeter

ERROR: In the video the instuctor simply runs go build -o greeter after re-building and the output to the terminal shows correctly. That was not my experience. The process I had to take was 1. make the modifications to the main.go prgoram 2. rebuild the program with go build -o greeter command at the terminal 3. I ran the main.go program with go run main.go after re-building and received a "out-of-bounds" error message. This is because since rebuilding the program you have to re-input the strings you want stored in the array. So 4. execute the ./greeter "hey" "ho" "howdy" commands in the terminal. 5. run the go run main.go command and everything works.

Results in ...

image

image