FAQ
Hello, gophers

I have a question about calling function from another package in go
assembly.

Say, there are too packages at the top level, foo and bar.

There is this function hello() defined in foo.

I would like to call foo.bar() from within an assembly file in bar. Is it
possible to do it?

Something like CALL foo·bar(SB) does not work.

Regards,
Hui


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Konstantin Khomoutov at Oct 22, 2015 at 11:09 am

    On Thu, 22 Oct 2015 02:51:51 -0700 (PDT) "Hui Z." wrote:

    Hello, gophers

    I have a question about calling function from another package in go
    assembly.
    There's no assemblies in Go -- that's a term from .NET.
    In Go, there are packages and executables (programs) which are made of
    packages: the package "main" which defines the program itself and
    typically several external packages -- standard or 3rd-party.
    Say, there are too packages at the top level, foo and bar.

    There is this function hello() defined in foo.

    I would like to call foo.bar() from within an assembly file in bar.
    Is it possible to do it?
    It's only possible to call the so-called exported package functions
    outside of the package's code itself. The rule Go uses for exporting
    anything is simple: every name which begins in a capital letter is
    exported, all the others are not. So if you name your function Bar()
    instear of bar(), if will become exported. Read [1] for more.

    Please make sure you master at least minimal amount of introductory
    material before attempting to program in an unknown language.
    Really, this topic is extensively covered in any introductory course.
    I'd start with [2] and then progressed to the "Effective Go" document
    (available online). The articles in the official Go blog ([1]) are
    also indispensable.

    And there's a good number of books, some of which are even freely
    available [3].

    1. https://blog.golang.org/package-names
    2. https://tour.golang.org
    3. https://github.com/golang/go/wiki/Books

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Chris dollin at Oct 22, 2015 at 11:20 am

    On 22 October 2015 at 12:08, Konstantin Khomoutov wrote:
    On Thu, 22 Oct 2015 02:51:51 -0700 (PDT)
    "Hui Z." wrote:
    Hello, gophers

    I have a question about calling function from another package in go
    assembly.
    There's no assemblies in Go -- that's a term from .NET.
    I thought the OP was asking about assembly (machine code)
    not assemblies, hence:

         CALL foo·bar(SB) does not work

    But I suspect that the actual answer is the same, viz, that
    you can only call exported (name-starting-with-a-capital)
    functions from assembly, just as you can only call exported
    functions from Go.

    Chris

    --
    Chris "allusive" Dollin

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Konstantin Khomoutov at Oct 22, 2015 at 11:55 am

    On Thu, 22 Oct 2015 12:19:57 +0100 "'chris dollin' via golang-nuts" wrote:
    I have a question about calling function from another package in go
    assembly.
    There's no assemblies in Go -- that's a term from .NET.
    I thought the OP was asking about assembly (machine code)
    not assemblies, hence:

    CALL foo·bar(SB) does not work

    But I suspect that the actual answer is the same, viz, that
    you can only call exported (name-starting-with-a-capital)
    functions from assembly, just as you can only call exported
    functions from Go.
    Oh, good catch, thanks!

    The usage of that '·' character should have rung a bell for me but
    didn't.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • peterGo at Oct 22, 2015 at 12:01 pm
    Konstantin,

    Your reply was nasty, uncalled for, and wrong. The question is not about C#
    assemblies, it's about Go assembler.

    Assembly language
    https://en.wikipedia.org/wiki/Assembly_language

    A Quick Guide to Go's Assembler
    https://golang.org/doc/asm

    Peter

    On Thursday, October 22, 2015 at 7:09:36 AM UTC-4, Konstantin Khomoutov
    wrote:
    On Thu, 22 Oct 2015 02:51:51 -0700 (PDT)
    "Hui Z." <zh.d...@gmail.com <javascript:>> wrote:
    Hello, gophers

    I have a question about calling function from another package in go
    assembly.
    There's no assemblies in Go -- that's a term from .NET.
    In Go, there are packages and executables (programs) which are made of
    packages: the package "main" which defines the program itself and
    typically several external packages -- standard or 3rd-party.
    Say, there are too packages at the top level, foo and bar.

    There is this function hello() defined in foo.

    I would like to call foo.bar() from within an assembly file in bar.
    Is it possible to do it?
    It's only possible to call the so-called exported package functions
    outside of the package's code itself. The rule Go uses for exporting
    anything is simple: every name which begins in a capital letter is
    exported, all the others are not. So if you name your function Bar()
    instear of bar(), if will become exported. Read [1] for more.

    Please make sure you master at least minimal amount of introductory
    material before attempting to program in an unknown language.
    Really, this topic is extensively covered in any introductory course.
    I'd start with [2] and then progressed to the "Effective Go" document
    (available online). The articles in the official Go blog ([1]) are
    also indispensable.

    And there's a good number of books, some of which are even freely
    available [3].

    1. https://blog.golang.org/package-names
    2. https://tour.golang.org
    3. https://github.com/golang/go/wiki/Books
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Mateusz Czapliński at Oct 22, 2015 at 6:15 pm

    On Thursday, October 22, 2015 at 11:51:51 AM UTC+2, Hui Z. wrote:
    Hello, gophers

    I have a question about calling function from another package in go
    assembly.
    Say, there are too packages at the top level, foo and bar.
    There is this function hello() defined in foo.
    I would like to call foo.bar() from within an assembly file in bar. Is it
    possible to do it?
    Something like CALL foo·bar(SB) does not work.
    Have a look at: http://stackoverflow.com/a/13490787/98528
    I think it *may* work if you write something like:

         CALL full∕path∕to∕foo·bar(SB)

    Although I'm not 100% sure. (Please note the special characters - the "∕"
    is not the same as a regular slash "/".)

    /Mateusz.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Hui Z. at Oct 23, 2015 at 6:36 am
    No luck. I tried what you suggest but it dose not work.

    The currently best way to do it is to write a local go function to wrap the
    imported function, and call the local go function in assembly.

    It seems it is not possible to import a package and call its function all
    in assembly, the assembler does not accept the statement import
    path/to/a/package
    On Friday, October 23, 2015 at 2:15:25 AM UTC+8, Mateusz Czapliński wrote:
    On Thursday, October 22, 2015 at 11:51:51 AM UTC+2, Hui Z. wrote:

    Hello, gophers

    I have a question about calling function from another package in go
    assembly.
    Say, there are too packages at the top level, foo and bar.
    There is this function hello() defined in foo.
    I would like to call foo.bar() from within an assembly file in bar. Is it
    possible to do it?
    Something like CALL foo·bar(SB) does not work.
    Have a look at: http://stackoverflow.com/a/13490787/98528
    I think it *may* work if you write something like:

    CALL full∕path∕to∕foo·bar(SB)

    Although I'm not 100% sure. (Please note the special characters - the "∕"
    is not the same as a regular slash "/".)

    /Mateusz.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Manlio Perillo at Oct 23, 2015 at 9:00 am
    Il giorno venerdì 23 ottobre 2015 08:36:25 UTC+2, Hui Z. ha scritto:
    No luck. I tried what you suggest but it dose not work.
    Did you tried with CALL foo·Bar(SB) ?

    Note the uppercase: Bar must be an exported function.
    [...]

    Regards Manlio

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Aram Hăvărneanu at Oct 23, 2015 at 9:32 am
    There are numerous examples in the standard library, especially in the
    runtime and in the syscall package.

    Contrary to misinformation in this thread, you *can* call unexported functions:

    : iridium:z; go env GOPATH
    /Users/aram
    : iridium:z; ls $GOPATH/src/z
    bar.s foo main.go
    : iridium:z;
    : iridium:z; ls $GOPATH/src/z/foo
    foo.go
    : iridium:z; pwd
    /Users/aram/src/z
    : iridium:z;
    : iridium:z; cat main.go bar.s
    package main

    import "z/foo"

    func bar()

    func main() {
         foo.Foo()
         bar()
    }

    TEXT ·bar(SB),7,$-8
         CALL z∕foo·bar(SB)
         RET

    : iridium:z;
    : iridium:z;
    : iridium:z; cat foo/foo.go
    package foo

    import "fmt"

    func Foo() {
         fmt.Println("hello from foo")
    }

    func bar() {
         fmt.Println("hello from bar")
    }

    : iridium:z; pwd
    /Users/aram/src/z
    : iridium:z; go build
    : iridium:z; z
    hello from foo
    hello from bar
    : iridium:z;

    --
    Aram Hăvărneanu

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matt Harden at Oct 23, 2015 at 5:48 pm
    Side note: I LIKE your shell prompt, Aram. It never occurred to me to make
    the prompt an effective no-op to the shell, at least not in the way you
    have. This way you can copy/paste entire lines in your shell and not have
    to remove the prompt. Do you do anything to sanitize the working directory
    so it won't contain shell metacharacters?
    On Fri, Oct 23, 2015 at 2:32 AM Aram Hăvărneanu wrote:

    There are numerous examples in the standard library, especially in the
    runtime and in the syscall package.

    Contrary to misinformation in this thread, you *can* call unexported
    functions:

    : iridium:z; go env GOPATH
    /Users/aram
    : iridium:z; ls $GOPATH/src/z
    bar.s foo main.go
    : iridium:z;
    : iridium:z; ls $GOPATH/src/z/foo
    foo.go
    : iridium:z; pwd
    /Users/aram/src/z
    : iridium:z;
    : iridium:z; cat main.go bar.s
    package main

    import "z/foo"

    func bar()

    func main() {
    foo.Foo()
    bar()
    }

    TEXT ·bar(SB),7,$-8
    CALL z∕foo·bar(SB)
    RET

    : iridium:z;
    : iridium:z;
    : iridium:z; cat foo/foo.go
    package foo

    import "fmt"

    func Foo() {
    fmt.Println("hello from foo")
    }

    func bar() {
    fmt.Println("hello from bar")
    }

    : iridium:z; pwd
    /Users/aram/src/z
    : iridium:z; go build
    : iridium:z; z
    hello from foo
    hello from bar
    : iridium:z;

    --
    Aram Hăvărneanu

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Aram Hăvărneanu at Oct 23, 2015 at 9:17 pm

    Side note: I LIKE your shell prompt, Aram. It never occurred to me
    to make the prompt an effective no-op to the shell, at least not
    in the way you have. This way you can copy/paste entire lines in
    your shell and not have to remove the prompt.
    You're the first person to mention it.

    Yes, that is exactly the reason. Acme and 9term make it really easy
    to select lines (just double click them), and they make it really
    easy to re-execute them; middle-click in acme and "send" in 9term.
    Do you do anything to sanitize the working directory so it won't
    contain shell metacharacters?
    No, I never had a problem with this. My PS1 is

         H="`/bin/hostname | sed 's/\..*$//'`"
         PS1='$(printf "%s" ": ${H}:`basename ${PWD}`; ")'

    --
    Aram Hăvărneanu

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 22, '15 at 9:51a
activeOct 23, '15 at 9:17p
posts11
users8
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase