user181548
user181548

Reputation:

How can I compile a Go program?

I got Go to compile:

0 known bugs; 0 unexpected bugs

and typed in the "hello world":

package main

import "fmt"

func main() {
  fmt.Printf("Hello, 世界\n")
}

Then I tried to compile it, but it wouldn't go:

$ 8c gotest2
gotest2:1 not a function
gotest2:1 syntax error, last name: main

This is going on on Ubuntu Linux on Pentium. Go installed and passed its tests. So where did I go wrong? Can someone tell me where to go from here?

I also tried this program:

package main

import fmt "fmt"  // Package implementing formatted I/O.


func main() {
    fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");
}

But this was also no go (must stop making go puns):

$ 8c gotest3.go
gotest3.go:1 not a function
gotest3.go:1 syntax error, last name: main

Upvotes: 20

Views: 16813

Answers (5)

kenorb
kenorb

Reputation: 166319

To compile Go code, use the following commands:

go tool compile gotest3.go # To create an object file.
go tool link -o gotest3 gotest3.o # To compile from the object file.
chmod +x gotest3 # To apply executable flag.
./gotest3 # To run the binary.

Upvotes: 0

Devendra Mukharaiya
Devendra Mukharaiya

Reputation: 610

use go run to run the go program. Here is the output.

$ cat testgo.go

package main

import "fmt"

func main() {
    fmt.Printf("Hello, 世界\n")
}

$go run testgo.go

Hello, 世界

Upvotes: 0

Kyle
Kyle

Reputation: 735

For Go 1.0+ the correct build command is now: go build

Upvotes: 42

VonC
VonC

Reputation: 1323223

(Update for Go1.0.x)

The section "Compile packages and dependencies" now list go build as the way to compile in go.
You still call 8g behind the scene, and the parameters you could pass to 8g are now passed with -gcflags.

-gcflags 'arg list'

arguments to pass on each 5g, 6g, or 8g compiler invocation

Upvotes: 3

Scott Wales
Scott Wales

Reputation: 11686

You're using 8c, which is the c compiler. 8g will compile go, and 8l will link.

Upvotes: 12

Related Questions