0% found this document useful (0 votes)
82 views28 pages

CSCI2730 BC Tut4 2024

This document provides a detailed guide for setting up a shared directory between a local PC and a virtual machine (VM), installing Visual Studio Code and Golang, and compiling and running Go programs. It includes step-by-step instructions for creating shared folders, installing necessary software, and managing Go modules and packages. Additionally, it offers useful online resources for further learning about Golang.

Uploaded by

idontknowthreek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views28 pages

CSCI2730 BC Tut4 2024

This document provides a detailed guide for setting up a shared directory between a local PC and a virtual machine (VM), installing Visual Studio Code and Golang, and compiling and running Go programs. It includes step-by-step instructions for creating shared folders, installing necessary software, and managing Go modules and packages. Additionally, it offers useful online resources for further learning about Golang.

Uploaded by

idontknowthreek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

The Chinese University of Hong Kong Computer Science & Engineering

CSCI2730: Introduction to blockchain and its applications


Lab4: Set up VSCode, install golang, and programming in
golang

1. Create a shared directory between local PC and VM

Click Devices -> Shared Folders -> Shared Folders Settings

1
The Chinese University of Hong Kong Computer Science & Engineering

Click Add new shared folder shown with + sign on blue folder

2
The Chinese University of Hong Kong Computer Science & Engineering

You need to add a folder path, folder name, check Auto-mount and check Make Permanent.

3
The Chinese University of Hong Kong Computer Science & Engineering

For folder path, click the scroll bar button and choose other to add a folder path from your local
PC. As you can see above, I created a directory “vmshared” inside my local drive E. You can
either first create directory and then click folder path or click folder path and then choose which
drive you want to create a shared folder in and then select that folder. I will select “vmshared”
folder.

4
The Chinese University of Hong Kong Computer Science & Engineering

From above, I first selected a folder on my local pc for folder path, then enter “shared” in folder
name (Make sure you have different folder names in ‘Folder Path’ and ‘Folder Name’). I have
folder name “vmshared” on my local pc and folder name “shared” on my VM. Check Automount
and check Make Permanent. Click OK.

5
The Chinese University of Hong Kong Computer Science & Engineering

You have a shared folder. Click OK.

6
The Chinese University of Hong Kong Computer Science & Engineering

Open your terminal and type command “su” then enter password and click ENTER. You are login
as root now.

7
The Chinese University of Hong Kong Computer Science & Engineering

Click Devices -> Insert Guest Additions CD Image…


That would download the required vboxsf shared settings.

8
The Chinese University of Hong Kong Computer Science & Engineering

After successful additions of Disk Image. Add your home user in vboxsf group. Your home user
name is what you see when you first open your terminal. In my case, I have vboxuser@ubuntu,
so my home user is “vboxuser”. After checking your home user, type command “sudo adduser
vboxuser vboxsf”. This would add the vboxuser in vboxsf group and then reboot your VM.

9
The Chinese University of Hong Kong Computer Science & Engineering

After reboot, open the terminal, type command “cd /media” press ENTER, then type command
“ls” press ENTER. You can see a folder sf_shared is created.

2. Install VSCode and Golang


https://code.visualstudio.com/docs/setup/linux

Click the above link and download .deb package.

10
The Chinese University of Hong Kong Computer Science & Engineering

As you can see from above, I downloaded the package in my local PC, now I can move the
downloaded the package in folder “vmshared” which I have shared with VM.

https://g/o.dev/dl/

11
The Chinese University of Hong Kong Computer Science & Engineering

Click the above link, and download .tar.gz package suitable for your architecture. (Make sure you download
the package according to your architecture AMD or ARM)

12
The Chinese University of Hong Kong Computer Science & Engineering

As you can see, I have downloaded go.tar.gz on my local PC, and moved to “vmshared” directory.

13
The Chinese University of Hong Kong Computer Science & Engineering

Type command “cd /media/sf_shared” then type command “ls”, you can see both code.deb and go.tar.gz
packages are available on VM.

Type command “sudo apt install ./code_1.82.2-1694671812_amd64.deb” press Enter, and that would
install the vscode in your VM.

14
The Chinese University of Hong Kong Computer Science & Engineering

Type command “code -v” press Enter, and you can see the vscode version installed on your VM.

15
The Chinese University of Hong Kong Computer Science & Engineering

Let’s unpack go package now. Type command “sudo cp go1.21.1.linux-amd64.tar.gz /usr/local”


press Enter, that would copy the go package in /usr/local directory/.

16
The Chinese University of Hong Kong Computer Science & Engineering

Type command “cd /usr/local” press Enter. Then type command “ls” press Enter, you can see we
have go.tar.gz package in /usr/local directory.

17
The Chinese University of Hong Kong Computer Science & Engineering

Now, Type command “sudo tar -xzf go1.21.1.linux-amd64.tar.gz” press Enter, and that would
unpack the go.tar.gz package.

18
The Chinese University of Hong Kong Computer Science & Engineering

After unpacking, type command “ls” press Enter, you can see we have “go” directory created.

19
The Chinese University of Hong Kong Computer Science & Engineering

Now, we need to add go binaries inside PATH environment variable. Type command “sudo
nano /etc/bash.bashrc” press Enter. You can also use vim or other editors instead of nano if you
are more familiar with those.

Scroll down to the end of file and add line “export PATH=$PATH:/usr/local/go/bin” then press
CTRL+O then press Enter and then press CTRL+X.

Finally, you can type command “source /etc/bash.bashrc” to incorporate the changes you have
made in bash.bashrc file in the current session.

20
The Chinese University of Hong Kong Computer Science & Engineering

Type command “go version” press Enter, and that would print the version of go installed on your
VM.

3. Compiling and running go programs

21
The Chinese University of Hong Kong Computer Science & Engineering

You can open a new terminal and create a directory “csci2730” and then enter csci2730
directory and then create lab4 directory and then enter lab4 directory, just I have done above.
The later lab section assumes you are in directory: “$PWD/csci2730/lab4”, then type command
“code .” and press Enter. This would open the VSCode IDE in your VM inside current working
directory.

3. Compile and run your code


If you just want to quickly compile and run your code, you can use `go run filename1.go
filename2.go … filenameN.go`. This will compile all files in a single main package and execute
the main() function.

22
The Chinese University of Hong Kong Computer Science & Engineering

You can open the vscode terminal inside VSCode and then execute commands inside that
terminal. Create file main.go by typing command “touch main.go” and then write the code as
written in above picture. Then run command “go run main.go” inside VSCode terminal. You can
see the similar output as shown in the above picture.

However, usually you have code divided into multiple files in single or multiple packages. If you
have single package and multiple *.go files, you can use go build to build all files into an
executable object and then run that object. For that, you will first have to create go module
using `go mod init module_name”

23
The Chinese University of Hong Kong Computer Science & Engineering

Above you can see, I created a module lab4 which is represented by go.mod file. For more
information on modules you can refer to: https://go.dev/blog/using-go-modules I now
have two files hello.go and main.go in a package main.

24
The Chinese University of Hong Kong Computer Science & Engineering

We use a single package main and multiple files in a package. In hello.go, I have function hello()
implemented, and main.go file call that function and prints its output. We can use `go run
main.go hello.go` to compile all the files and invoke main() function and get the output of main()
function, but what if the number of files are very large? It is more convenient to first build and
then run. We can use `go build` to build all the files into an executable object, and then execute
that object.

25
The Chinese University of Hong Kong Computer Science & Engineering

As you can see from above, we have lab4.exe file (you may different executable filename,
depends on your working directory name). Now, you can execute the executable file using
`./lab4`.

Let’s now create different packages and let one package import another package to use its
functionality.

Each individual package is identified by its directory, so make sure you first create directory by
the package name you want, and then insert *.go files inside that directory with each go file
using the package name same as the directory name.

You can see we first create “hello” directory and then create hello.go file inside that directory.
We use package hello here, and the function name starts with capital letter to export this
function for other packages to use.

26
The Chinese University of Hong Kong Computer Science & Engineering

Now, if we want to use the function Hello() from hello package, we can first import using
“module_name/hello”, in this case, we have “lab4/hello”. As you can see, we use package name
and then invoke exported functions for this package. Hello is exported by package hello, so we
can invoke it in our main package.

Above is the final output. We first build using `go build’ then run the executable.

Useful Online references to learn Golang:


https://gobyexample.com/
https://go.dev/doc/tutorial/getting-started

https://www.w3schools.com/go/

Thank you!
Questions?

27
The Chinese University of Hong Kong Computer Science & Engineering

28

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy