https://play.golang.org/p/vQs1J-MnZFE
Specifically,
“First”
“Last”
“Age”
In Go, capital items indicate they can be exported. That means it can be accesed from another package
Here’s a bit more in the language specification:
https://golang.org/ref/spec#Exported_identifiers
What’s the difference between import and export. I’m pretty sure that import means that I use some prewritten package in my program. So does export mean that I am sending part of my code somewhere?
No, it is related if items in the package can be accesed from another.
Import specify which packages you will use. export means if you can acess or not to an item (in another programming languages it is called private and public access)
Take a look at this: https://play.golang.org/p/bOficacMcY9
It uses a new feature of the Go Playground. Now you can include more than one file of source code. Where you see
-- go.mod --
The part after that would normally be in a file called go.mod, until you see
-- say/say.go --
and the part after that would be in a file called say.go
inside a directory (folder) called say
. So there are three files represented in this Go Playground example.
In say.go
, another package called say
is defined. In that, there are two functions, Hello()
and Name()
. To use them from package main
, you need to capitalize them, as shown.
In package main
, you need an import
statement to gain access to package say
, and then functions in that package can be called as say.Hello()
and say.Name()
.
Try changing Hello()
to hello()
in either or both of package main
and package say
and see what happens.
In package main
, package say
is imported by using the import
statement.
In package say
, the functions are exported by capitalizing their names. Other things besides functions may also be exported from packages.
Studying this…
The files being go.mod, say, and say.go?
What’s module playground?
import "playground/say"
This is interesting. playground is a package? say is a specified part of that package? Oh. You say “say” is a package.
%s is new to me
%s the uninterpreted bytes of the string or slice (Go doc)
What does uninterpreted mean here?
fmt.Printf("Hello, ") Here, why did you use Printf rather than Pritnln?
How can I find the package say on golang.org?
Maybe I need a good definition of a package. I did a little searching with google and I think I’m getting it.
"The part after that "… do you mean module playground by this?
Try changing Hello()
to hello()
in either or both of package main
and package say
and see what happens…did it. Fun! and interesting.
I think I’m done for now.
A little more… can you give me definitions for import and export?
Don’t worry about the module stuff and filenames in my example. I used the Go Playground because I needed a way to answer your question about imports and exports.
This reminds me to nag you again about learning to use Terminal (actually, the Bash shell) so you can start using Go for real. The Playground is just a sandbox to play around in, and works differently than an actual programming environment.
By learning Go without first knowing things that support it, it is like building the second story of a house first.
I’ve forgotten what to do with the terminal
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.