LoginSignup
1
3

More than 3 years have passed since last update.

Go言語で修飾名が重複したときのエラー

Posted at

エラー箇所

こんなエラーがあります.
"main redeclared in this block
previous declaration at..."

とりあえず,こんなエラーが出た場合の処置だけ.


[Running] go run "c:\Users\shell\PycharmProjects\go_programming\practice1.go"
# command-line-arguments
.\practice1.go:17:1: syntax error: non-declaration statement outside function body
.\practice1.go:21:6: main redeclared in this block
    previous declaration at .\practice1.go:9:6

[Done] exited with code=2 in 0.695 seconds

"main redeclared in this block previous declaration at" とあるように,goファイル中で"main"が重複して用いられているようです...

元のコード

main.go
package main

import (
    "fmt"
    "os/user"
    "time"
)

func main() {

    fmt.Println("Lets Go lang.", time.Now())
    fmt.Println(user.Current)
}


fmt.Println中のtime, user が重複されているという事でしょうか...? (正直,原因が完全にはわかりません)

とりあえずの解決策

main.go
package main

import (
    "fmt"
    USER "os/user"
    Time "time"
)

func main() {

    fmt.Println("Lets Go lang.", Time.Now())
    fmt.Println(USER.Current)
}

インポート中で,別名で定義すれば,とりあえずできました.
詳しいことご存じの方いらっしゃいましたら,教えていただけますと幸いです.

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3