L
L
Luke LikeSkywalker2018-09-15 22:17:45
go
Luke LikeSkywalker, 2018-09-15 22:17:45

Why do I have only 1 element in os.Args?

I'm learning from a book by Donovan Kernighan.
when i execute the command

package main

import (
  "fmt"
  "os"
)

func main() {
  var s, sep string

  for _, arg := range os.Args[1:] {
    s += sep + arg
    sep = " "
  }

  fmt.Println(s)
}

I get an empty line
Command go run main.go
and when I display os.Args itself, then only a slice with one element is displayed
[/var/folders/g8/7bzt3n1932s92_5z09zytnr40000gn/T/go-build305367393/b001/exe/server]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rustler2000, 2018-09-15
@oolooq

Because go run compiles and runs. You don't specify additional parameters. Do go run main.go some args

A
Andrey Burov, 2018-09-15
@BuriK666

You need https://golang.org/pkg/strings/#Join

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question