Go and Julia Packages and Modules Compared

Comparing how Julia and Go deal with dependencies to other code

Image for post
Image for post
Gopher image by Ashley McNamara CC BY-NC-SA 4.0
using Greetings
message = Greetings.hello("Gladys")
println(message)
name = "Hello"
uuid = "f4029ba5-1bab-490d-8bbe-d6d1c8a1f0a8"
authors = ["Erik Engheim <erik.engheim@mac.com>"]
version = "0.1.0"

[deps]
Greetings = "1ef576d1-6215-4f11-828a-3bafa7859891"
[[Greetings]]
path = "../Greetings"
uuid = "1ef576d1-6215-4f11-828a-3bafa7859891"
version = "0.1.0"
module Greetings

using Printf

export hello

function hello(name::AbstractString)
message = @sprintf("Hi, %s. Welcome!", name)
return message
end

end # module
shell> tree Greetings/
Greetings/
├── Manifest.toml # Where you find deps
├── Project.toml # Direct dependencies
└── src
└── Greetings.jl

Go Modules and Package

package greetings

import "fmt"

// Hello returns a greeting for the named person.
func Hello(name string) string {
// Return a greeting that embeds the name in a message.
message := fmt.Sprintf("Hi, %v. Welcome!", name)
return message
}
greetings/
├── go.mod # Defines module
└── greet.go # Our source code
module example.com/greetings

go 1.13
.
├── greetings
│ ├── go.mod
│ └── greet.go
└── hello
├── go.mod
└── hello.go
package main

import (
"fmt"

"example.com/greetings"
)

func main() {
// Get a greeting message and print it.
message := greetings.Hello("Gladys")
fmt.Println(message)
}
module hello

go 1.13

replace example.com/greetings => ../greetings
[[Greetings]]
path = "../Greetings"
uuid = "1ef576d1-6215-4f11-828a-3bafa7859891"
version = "0.1.0"
$ go build
module hello

go 1.13

replace example.com/greetings => ../greetings

require example.com/greetings v0.0.0-00010101000000-000000000000

Comparison of Go and Julia Module and Package Commands

Initialize

pkg> generate Greetings
$ go mod init example.com/greetings
module Greetings
include("foo.jl")
include("bar.jl")
end

Testing

$ go test
pkg> activate .
(Greetings) pkg>
(Greetings) pkg> test
pkg> test Greetings

List Dependencies

$ go list -m all
hello
example.com/greetings v0.0.0-00010101000000-000000000000 => ../greetings
$ cd Hello/
$ julia
julia> ]
pkg> activate .
Activating environment at `~/Development/Hello/Project.toml`

(Hello) pkg> status
Project Hello v0.1.0
Status `~/Development/Hello/Project.toml`
[1ef576d1] Greetings v0.1.0 `../Greetings`

Adding Dependencies

$ go get rsc.io/sampler@v1.3.1

$ cat go.mod
module hello

go 1.13

replace example.com/greetings => ../greetings

require (
example.com/greetings v0.0.0-00010101000000-000000000000
rsc.io/sampler v1.3.1 // indirect
)
(Hello) pkg> add Tar@0.1.0
shell> cat Project.toml
name = "Hello"
uuid = "f4029ba5-1bab-490d-8bbe-d6d1c8a1f0a8"
authors = ["Erik Engheim <erik.engheim@mac.com>"]
version = "0.1.0"

[deps]
Greetings = "1ef576d1-6215-4f11-828a-3bafa7859891"
Tar = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
shell> cat Manifest.toml
# This file is machine-generated - editing it directly is not advised

[[Greetings]]
deps = ["Printf"]
path = "../Greetings"
uuid = "1ef576d1-6215-4f11-828a-3bafa7859891"
version = "0.1.0"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[Tar]]
git-tree-sha1 = "5a5ba451fb83875247e22216609b25b581cddaad"
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
version = "0.1.0"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
$ cat go.sum
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZOaTkIIMiVjBQcw93ERBE4m30iBm00nkL0i8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/sampler v1.3.1 h1:F0c3J2nQCdk9ODsNhU3sElnvPIxM/xV1c/qZuAeZmac=
rsc.io/sampler v1.3.1/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=

Tidy Dependencies

$ go mod tidy
$ cat go.mod
module hello

go 1.13

replace example.com/greetings => ../greetings

require example.com/greetings v0.0.0-00010101000000-000000000000
$ cat go.sum
$

Info About What Package Provides

$ go doc rsc.io/quote/v3
package quote // import "rsc.io/quote"

Package quote collects pithy sayings.

func Concurrency() string
func GlassV3() string
func GoV3() string
func HelloV3() string
func OptV3() string
[plutonium hello] $

Conclusion

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store