G
G
gkozyrev2019-11-07 07:11:04
go
gkozyrev, 2019-11-07 07:11:04

Golang not running main()?

I am writing a small server to get addresses of microservices.
The server consists of several directories that are imported, as well as the main files main.go and server.go
main.go:

package main

import (
  "fmt"
)

const (
  port = ":7777"
)

func main() {
  fmt.Println("Starting Servers")
  serve()
}

server.go:
package main

import (
  "./data_storage"
  pb "./proto"
  "./platform"
  "./user"
  "fmt"
  "google.golang.org/grpc"
  "log"
  "net"
)

func serve() {
  lis, err := net.Listen("tcp", port)
  if err != nil {
    log.Fatalf("failed to listen: %v", err)
  }
  s := grpc.NewServer()

  // ----- User -----
  fmt.Println("\nRegistering User Address Server")
  pb.RegisterUserStackServer(s, &user.Server{})

  // ----- Platform -----
  fmt.Println("\nRegistering Platform Address Server")
  pb.RegisterPlatformStackServer(s, &platform.Server{})

  // ----- DataStorage -----
  fmt.Println("\nRegistering DataStorage Address Server")
  pb.RegisterDataStorageStackServer(s, &data_storage.Server{})

  if err := s.Serve(lis); err != nil {
    log.Fatalf("failed to serve: %v", err)
  }
}

Essence of the question:
According to the principles of Golang, the main () function inside the main.go file is run first in any case. So if I just run go run main.go or go build main.go, then main () tries to start and gives some kind of output.
But when I start building the entire server through go build, or in the hope of seeing some kind of output in the form of an error, I run ggo run main.go server.go, I get nothing at the output: no prints that go to the very first line, no errors, everything is like started, but there are no signs of life.
What could be the problem? Maybe I don't understand something?
Thanks in advance everyone!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Samsonov, 2019-11-07
@bitniks

go runcompiles and runs the program https://golang.org/cmd/go/#hdr-Compile_and_run_Go_...
go build only compiles https://golang.org/cmd/go/#hdr-Compile_and_run_Go_... The compiled program can then be run as regular executable
When you run go run main.goor go build main.go, you get what you want. In the first case, there will be compilation and launch, in the second, only compilation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question