# I'm simply trying to import a function from another file

**URL:** <https://forum.golangbridge.org/t/im-simply-trying-to-import-a-function-from-another-file/34376>\
**Category:** Getting Help\
**Created:** [February 1, 2024, 12:26pm UTC](https://forum.golangbridge.org/t/im-simply-trying-to-import-a-function-from-another-file/34376 "2024-02-01T12:26:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alkapwn3d](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/alkapwn3d/32/14701_2.png) [@alkapwn3d](https://forum.golangbridge.org/u/alkapwn3d)\
**Post date:** [February 1, 2024, 12:26pm UTC](https://forum.golangbridge.org/t/im-simply-trying-to-import-a-function-from-another-file/34376/1 "2024-02-01T12:26:50Z")

</div>

Sorry if this question is too basic. I just started learning Go yesterday

I’m trying to import a function from another file but I get a console error saying the function is undefined. Cant figure out what I’m doing wrong.

I pushed it to my github repo for easy viewing : [alkapwn3d/vending-machine-go](https://github.com/alkapwn3d/vending-machine-go)

---

<div class="post-metadata">

**Author:** ![pekim](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/pekim/32/14094_2.png) [@pekim](https://forum.golangbridge.org/u/pekim)\
**Post date:** [February 1, 2024, 3:10pm UTC](https://forum.golangbridge.org/t/im-simply-trying-to-import-a-function-from-another-file/34376/2 "2024-02-01T15:10:47Z")

</div>

“`go run vending-machine.go`” will only compile that one file. Whereas “`go run .`” will compile and run all of the `.go` source files in the directory.

`go help run`

> Run compiles and runs the named main Go package.  
> Typically the package is specified as a list of .go source files from a single  
> directory, but it may also be an import path, file system path, or pattern  
> matching a single known package, as in ‘go run .’ or ‘go run my/cmd’.

---

<div class="post-metadata">

**Author:** ![alkapwn3d](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/alkapwn3d/32/14701_2.png) [@alkapwn3d](https://forum.golangbridge.org/u/alkapwn3d)\
**Post date:** [February 1, 2024, 5:17pm UTC](https://forum.golangbridge.org/t/im-simply-trying-to-import-a-function-from-another-file/34376/3 "2024-02-01T17:17:42Z")

</div>

Duh. I should have known, but that makes sense.  
Many thanks kind sir.

---

<div class="post-metadata">

**Author:** ![alexcray](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/a/9e8a1a/32.png) [@alexcray](https://forum.golangbridge.org/u/alexcray)\
**Post date:** [February 4, 2024, 2:39pm UTC](https://forum.golangbridge.org/t/im-simply-trying-to-import-a-function-from-another-file/34376/4 "2024-02-04T14:39:31Z")

</div>

- Save this file in the same directory as the file where you want to use the function(s). If it’s in a different directory, you’ll need to adjust the import path accordingly.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [May 4, 2024, 2:40pm UTC](https://forum.golangbridge.org/t/im-simply-trying-to-import-a-function-from-another-file/34376/5 "2024-05-04T14:40:24Z")

</div>

Closed on May 4, 2024, 2:40pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
