FAQ
I have "runtime error: index out of range" error in the following code:
package main

import (

     "fmt"

     "os"

)


func main() {



     if len(os.Args) >= 2 && os.Args[2] == "-l" {

         fmt.Println("-l")

     }


}

What's wrong with logical AND expression? It should've evaluate left part (
len(os.Args) >= 2) to "false" and exit...

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jan Mercl at Jan 27, 2015 at 5:20 pm

    On Tue Jan 27 2015 at 18:17:49 Victor L wrote:

    if len(os.Args) >= 2 && os.Args[2] == "-l" { ...
    What's wrong with logical AND expression? It should've
    evaluate left part (len(os.Args) >= 2) to "false" and exit...
    Slice indexes are zero based. element at index 2 exists only if len >= 3.

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Klaus Post at Jan 27, 2015 at 5:20 pm
    Sorry... you are off by one ;)

    It should be ">2".

    /Klaus
    On Tuesday, January 27, 2015 at 6:17:55 PM UTC+1, Victor L wrote:


    I have "runtime error: index out of range" error in the following code:
    package main

    import (

    "fmt"

    "os"

    )


    func main() {



    if len(os.Args) >= 2 && os.Args[2] == "-l" {

    fmt.Println("-l")

    }


    }

    What's wrong with logical AND expression? It should've evaluate left part (
    len(os.Args) >= 2) to "false" and exit...
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Klaus Post at Jan 27, 2015 at 5:22 pm
    It should be ">2".
    ... or os.Args[1]

    (or you should have a look at the "flag" package which does all this for
    you)

    /Klaus

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 27, '15 at 5:17p
activeJan 27, '15 at 5:22p
posts4
users3
websitegolang.org

3 users in discussion

Klaus Post: 2 posts Victor L: 1 post Jan Mercl: 1 post

People

Translate

site design / logo © 2022 Grokbase