update: write your first program

gopath is no longer used.
This commit is contained in:
İnanç Gümüş
2021-03-23 16:47:38 +03:00
committed by GitHub
parent eb4848a07b
commit 2f920b497c

View File

@ -34,57 +34,22 @@ Enjoy!
* While inside a program directory, type: * While inside a program directory, type:
* `go run main.go` * `go run main.go`
## WHAT IS $GOPATH?
* _$GOPATH_ is an environment variable which points to a directory where the downloaded and your own Go files are stored.
* **On Windows**, it's in: `%USERPROFILE%\go`
* **On OS X & Linux**, it's in: `~/go`
* **NOTE:** Never set your GOPATH manually. It's by default under your users directory.
* **GOPATH has 3 directories:**
* **src:** Contains the source files for your own or other downloaded packages. You can build and run programs while in a program directory under this directory.
* **pkg:** Contains compiled package files. Go uses this to make the builds (compilation & linking) faster.
* **bin:** Contains compiled and executable Go programs. When you call go install on a program directory, Go will put the executable under this folder.
* _You might want to add this to your `PATH` environment variable if it's not there already._
## WHERE YOU SHOULD PUT YOUR SOURCE FILES? ## WHERE YOU SHOULD PUT YOUR SOURCE FILES?
* `$GOPATH/src/github.com/yourUsername/programDirectory` * In any directory you like.
* **Example:**
* My GitHub username is: inancgumus
* So, I put all my programs under: `~/go/src/github.com/inancgumus/`
* And, let's say that I've a program named hello, then I put it under this directory: `~/go/src/github.com/inancgumus/hello`
## FIRST PROGRAM ## FIRST PROGRAM
* **Create directories:** ### Create a directory
* **OS X & Linux (or git bash):** * Create a new directory:
* Create a new directory: * `mkdir myDirectoryName`
* `mkdir -p ~/go/src/yourname/hello` * Go to that directory:
* Go to that directory: * `cd myDirectoryName`
* `cd ~/go/src/yourname/hello`
* **Windows:** ### Add the source code files
* Create a new directory: * Create a new `code main.go` file.
* `mkdir c:\Go\src\yourname\hello` * This is going to the create the file in the folder, and open up it in the Visual Studio Code.
* Go to that directory:
* `cd c:\Go\src\yourname\hello`
* Create a new `code main.go` file under Visual Studio Code.
* And add the following code to it and save it. * And add the following code to it and save it.
* Then, return back to the command-line.
* Run it like this: `go run main.go`
```go ```go
package main package main
@ -96,12 +61,14 @@ func main() {
} }
``` ```
### Run the program
* Finally, return back to the command-line.
* Run it like this: `go run main.go`
* If you create other files and run them all, you can use this command:
* `go run .`
That's all! Enjoy! That's all! Enjoy!
## NOTE:
* There is a new *Go Modules* support that allows you to run your programs in any directory that you want. It's still an experimental feature, when it stabilizes, I'll update the course and include Go Modules as well.
> For more tutorials: [https://blog.learngoprogramming.com](https://blog.learngoprogramming.com) > For more tutorials: [https://blog.learngoprogramming.com](https://blog.learngoprogramming.com)
> >
> Copyright © 2018 Inanc Gumus > Copyright © 2018 Inanc Gumus