Finish the Day Two post
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 2m6s
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 2m6s
This commit is contained in:
parent
5eab7bcb9d
commit
5aa099665c
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
title: ""Learning Go: Day Two"
|
title: "Learning Go: Day Two"
|
||||||
date: 2024-05-02T08:00:00.0Z
|
date: 2024-05-02T08:00:00.0Z
|
||||||
tags:
|
tags:
|
||||||
- tech
|
- tech
|
||||||
@ -27,8 +27,70 @@ Fairly straightforward, right?
|
|||||||
|
|
||||||
## Importing and running the code
|
## Importing and running the code
|
||||||
|
|
||||||
|
So, I can't just do `import "maths"`, because that doesn't seem to resolve. Instead, I have to refer to my entire module name, and then append the path to it:
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "lewisdale.dev/learn-go/maths"
|
||||||
|
```
|
||||||
|
|
||||||
|
So now I should be able to use my imported module and call `maths.multiply`, right?
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
fmt.Println(sayHello())
|
||||||
|
num := maths.multiply(2, 5)
|
||||||
|
fmt.Printf("2 * 5 = %d\n", num)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go run main.go
|
||||||
|
> ./main.go:16:15: undefined: maths.multiply
|
||||||
|
```
|
||||||
|
|
||||||
## Wait, what?
|
## Wait, what?
|
||||||
|
|
||||||
|
So, it turns out in Go, there's no explicit export keyword. Instead, if you want a function to be exported [you have to capitalise the first letter](https://go.dev/tour/basics/3). Lowercase functions are implicitly private.
|
||||||
|
|
||||||
|
```go
|
||||||
|
// maths.go
|
||||||
|
package maths
|
||||||
|
|
||||||
|
func Multiply(a, b int) int {
|
||||||
|
return a * b
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
And then we can do
|
||||||
|
|
||||||
|
```go
|
||||||
|
// main.go
|
||||||
|
func main() {
|
||||||
|
fmt.Println(sayHello())
|
||||||
|
num := maths.Multiply(2, 5)
|
||||||
|
fmt.Printf("2 * 5 = %d\n", num)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
As a useful extra, the Go plugin on VS Code[^2] even organised my imports into one `import ()` statement:
|
||||||
|
|
||||||
|
```go
|
||||||
|
// main.go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"lewisdale.dev/learn-go/maths"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
## The vendor directory
|
## The vendor directory
|
||||||
|
|
||||||
|
When I was looking up how to create and import modules, I did find [this useful StackOverflow comment](https://stackoverflow.com/a/45813698) that mentions that Go > 1.5 has support for a `vendor` directory, that allows you to put code inside a `vendor` directory, and Go will lookup the package without requiring the local module prefix.
|
||||||
|
|
||||||
|
I'm not going to use this, just because I'm probably going to reinvent the wheel a few times over the course of this series and I don't know the standard library well enough[^3] to trust myself to not accidentally tread all over it, but it's good to know it's there.
|
||||||
|
|
||||||
[^1]: Yes, maths, not math
|
[^1]: Yes, maths, not math
|
||||||
|
[^2]: It was less helpful when it kept auto-deleting my import before I did the export though, had to use Vim just to make my point.
|
||||||
|
[^3]: Or like, at all.
|
@ -2,7 +2,7 @@
|
|||||||
--color-primary: var(--color-neutral-200);
|
--color-primary: var(--color-neutral-200);
|
||||||
--color-secondary: var(--color-neutral-800);
|
--color-secondary: var(--color-neutral-800);
|
||||||
--color-decoration: var(--color-amber-200);
|
--color-decoration: var(--color-amber-200);
|
||||||
--color-subtle: var(--color-zinc-400);
|
--color-subtle: var(--color-neutral-300);
|
||||||
--color-disabled: var(--color-neutral-400);
|
--color-disabled: var(--color-neutral-400);
|
||||||
--color-accent: var(--color-zinc-600);
|
--color-accent: var(--color-zinc-600);
|
||||||
|
|
||||||
@ -60,7 +60,7 @@
|
|||||||
--color-primary: var(--color-neutral-200);
|
--color-primary: var(--color-neutral-200);
|
||||||
--color-secondary: var(--color-neutral-800);
|
--color-secondary: var(--color-neutral-800);
|
||||||
--color-decoration: var(--color-amber-200);
|
--color-decoration: var(--color-amber-200);
|
||||||
--color-subtle: var(--color-zinc-400);
|
--color-subtle: var(--color-neutral-300);
|
||||||
--color-disabled: var(--color-neutral-400);
|
--color-disabled: var(--color-neutral-400);
|
||||||
--color-accent: var(--color-zinc-400);
|
--color-accent: var(--color-zinc-400);
|
||||||
--color-callout-bg: var(--color-slate-800);
|
--color-callout-bg: var(--color-slate-800);
|
||||||
@ -71,7 +71,7 @@
|
|||||||
--color-primary: var(--color-neutral-200);
|
--color-primary: var(--color-neutral-200);
|
||||||
--color-secondary: var(--color-neutral-800);
|
--color-secondary: var(--color-neutral-800);
|
||||||
--color-decoration: var(--color-amber-200);
|
--color-decoration: var(--color-amber-200);
|
||||||
--color-subtle: var(--color-zinc-400);
|
--color-subtle: var(--color-neutral-300);
|
||||||
--color-disabled: var(--color-neutral-400);
|
--color-disabled: var(--color-neutral-400);
|
||||||
--color-accent: var(--color-zinc-600);
|
--color-accent: var(--color-zinc-600);
|
||||||
--color-callout-bg: var(--color-slate-800);
|
--color-callout-bg: var(--color-slate-800);
|
||||||
|
Loading…
Reference in New Issue
Block a user