Length for flag.Args()

915 views
Skip to first unread message

Amitabh Arya

unread,
Mar 16, 2012, 7:45:02 PM3/16/12
to golan...@googlegroups.com
flag.Args() return []string.

How can we get the length of this array. To get the total flags passed by user on terminal.

Robert Bloomquist

unread,
Mar 16, 2012, 8:04:56 PM3/16/12
to golan...@googlegroups.com

The total number of flags is flag.NFlag().

The length of the array returned by flag.Args(), that is, the total number of arguments, is flag.NArg() or len(flag.Args()).

rif

unread,
Mar 16, 2012, 8:05:29 PM3/16/12
to golan...@googlegroups.com
You mean like this?

package main

import ("fmt"; "flag")

func main() {
flag.Parse()
flags := flag.Args()
fmt.Println(len(flags))

Amitabh Arya

unread,
Mar 16, 2012, 8:18:20 PM3/16/12
to rif, golan...@googlegroups.com
Right, this is what I want. Sorry, I am new to GO :)

Thanks,
Amitabh Arya

Hotei

unread,
Mar 16, 2012, 10:02:48 PM3/16/12
to golan...@googlegroups.com
If you spend a little time with the tutorials  (by tutorials I mean the Learning GO section on http://tip.golang.org/doc/)  That will explain a lot of what you need.  Go is actually a pretty simple language (compared to some). The people who did the beginner level documents put a lot of effort into them and its worth giving them a close look.  They certainly helped me translate a C/C++ background over to the "go" way of thinking/coding.  They assume a familiarity with some previous programming language but they answer LOTS and LOTS of things you'll eventually need to use go effectively.  Since you have go installed there are also the go sample programs in the "test" code under the source tree.  Don't be afraid to look there once you've FULLY explored the tutorials.  Go programs tend to be quite small and easily understood (well - usually - there are always exceptions :-)  And there are a TON of program examples on the various public repositories that you can examine for examples of usage.


On Friday, March 16, 2012 7:45:02 PM UTC-4, Amitabh Arya wrote:

Matthew

unread,
Mar 17, 2012, 12:02:21 AM3/17/12
to golan...@googlegroups.com
For my part, I found http://tour.golang.org/ to be very, very helpful in providing a structured way to explore some basic language concepts. I overlooked it the first time I encountered it, I'm sad to say; I missed the fact that it included exercises. 

Once I realized my mistake, I went through all of them except the last one. I felt like I'd gained a much better feel for writing it than I ever had in my abortive attempts at "toy" projects.

Amitabh Arya

unread,
Mar 19, 2012, 11:51:42 AM3/19/12
to golan...@googlegroups.com
Yep, following URLs really help me


Almost ready with my small web server  which read files and show on webpage, Thanks to you guys. After this I am going to write real web server for people.
Reply all
Reply to author
Forward
0 new messages