Home > Mobile >  Can I define my own runtime paramet like GOOS or GOARCH in golang?
Can I define my own runtime paramet like GOOS or GOARCH in golang?

Time:08-09

For some reason I want to use a paramet which define when building. Such as GOLIB=A go build or GOLIB=B go build, and use it like:

if runtime.GOLIB == "A"{
    //dosomething
}
if runtime.GOLIB == "B"{
    //dosomething
}

Is there anyway to did it?

Modify:

The really problem is that I have a new OS (suppoose called A ) which is similar with linux but has some different I/O operation. So I want to define my own class like fd_A.go,syscall_A.go(just like fd_unix.go/fd_windows.go and syscall.unix.go/syscall.windows.go),which implement the method like Bind,Accept,Recvform,Connectetc.

And the upper method such asUDP,TCP,HTTP will use the method I define when build project use go build -tags A, but using origin method without -tags A.

CodePudding user response:

Is there anyway to did it?

No, at least not how you think this stuff works. (You cannot modify package runtime.)

You have two options:

  • Build Tags and files defining appropriate values based on the build tag used.
  • Inject a value during linkage with -ldflags "-X 'your/package/path.VariableName=YourValueHere'

This sound like an XY problem.

  •  Tags:  
  • go
  • Related