rodriggj / Go

0 stars 0 forks source link

1.1 Installation & Resources #1

Open rodriggj opened 2 years ago

rodriggj commented 2 years ago

Installation

  1. Go to this link youtube
  2. Go to Go Lang documentation page golang.org
rodriggj commented 2 years ago

Demo

  1. go to How to Write Go Code How to Write Go Code

  2. Install the Go Module in Visual Studio Code

  3. Now you need to create a Module

    go mod init example.com/go-demo-1

    TERM: In Go modules are elements of distribution and versioning

NOTE: Typically you want the name of the module to be the location on the internet where you will download the module. Since we don't have one yet, we'll use a generic example.com/go-demo-1

  1. When you run the previous command, GO will create a go.mod file which you can see in the directory structure.

  2. Within our project directory of we now need to create a GO Package, which is just a folder that contains GO code. You can name the folder what ever you like, in this example we will name the folder mascot.

    mkdir mascot
  3. Within the mascot folder create a file called mascot.go. This will trigger the GO Extension to kickoff and when it does it will find that it is missing a few controls.

    cd mascot
    touch mascot.go
  4. At the bottom-right of the Visual Studio Code editor you should see a dialog box appear prompting for an installation of some additional packages.

An installation process will kick-off in the console and display output similar to the following when complete:

  1. Once this process completes you can begin to start writing code in your mascot.go file.
    • The first thing you'll need is a package declaration
    • Then you can write a simple function

NOTE: The syntax for the function signature is, func to indicate a function, function name which is BestMascot(), and return value data type which is string.

  1. Now we need to write a main function. We can call this main.go and place in the root level of the project directory
cd ..
touch main.go
  1. Within the main.go function write the following code
    • Begin by making a package declaration. Visual Studio Code through autopopulation will suggest main for the package.
    • Next, write the func statement
    • Within the body of the function you will call the format package using fmt. VS Code will initiate an import block and auto-import the correct fmt package.
    • Using dot notation, you want to call the Println() function and pass as a param the mascot package, and using dot notation call the BestMascot() function. VS Code will again auto-import the example.com/go-demo-1 package_.

  1. Now we can run our main program. Open a terminal session, nav to the root directory of the project, and enter the following command:
    go run main.go

And you should see the return statement "Tut" like we programmed into our BestMascot() function.

rodriggj commented 2 years ago

Import External Packages

  1. If you wanted to import additional external packages into your main application you would follow a process similar to the following.

    For example, if we wanted to import the "quote" package. This package is not installed in our local env so Visual Studio immediately indicates an "error" with the red-squiggly line

  1. To fix this within the main code block, we can add an import of the required package to our import block, but referencing the url of the package required for the quote functionality.

  1. Now the main block is fixed, but the import block is in error. To fix this we can use VS Code Quick Fix by hovering over the red-line and selecting the Quick Fix. VS Code will then download the appropriate package to our local and correct the error.

The go.sum package has now been downloaded to our local directory structure, and upon a "Save" of the file, the error goes away, along with an additional file "go.sum" added to your project.

  1. Now if we run the main application again we should see the output of the quote function.

rodriggj commented 2 years ago

Writing Tests

  1. We can add tests to our code functionality by entering a new file with our automated tests. Here within the mascot directory, add a new file and add test to the file name like so:

    cd mascot
    touch mascot_test.go
  2. Now within the _mascottest.go file enter the following code:

  1. VS Code has detected that this is a test file and has provided an option to run test or debug. Here we can simply click run test.

  2. As expected the test failed, because we hardcoded "Tut" in our BestMascot() function and we were testing to see if the return value was "Go Gopher". The terminal output reflects the test failure.

  1. We can fix this bug with VS Code, by right clicking on test function, and then selecting "Go to Definition", which will refer us to the BestMascot() function in our mascot.go file. Here if we now update the hard-coded return value of "Tut" to "Go Gopher", and rerun our test we should see a successful test case.