I'm having issues importing "golang.org/x/net/html" it's simply telling me it can't find the package. Looking around the internet has only confused me more with some people saying that you can't use it anymore, some people saying to do this: Install exp/html in Go and some people saying that just using "golang.org/x/net/html" works just fine.
Here is just a small part of the code I'm trying to run:
package main
import (
"fmt"
"html"
"net/http"
"golang.org/x/net/html"
"os"
"strings"
)
// Helper function to pull the href attribute from a Token
func getHref(t html.Token) (ok bool, href string) {
// Iterate over all of the Token's attributes until we find an "href"
for _, a := range t.Attr {
if a.Key == "href" {
href = a.Val
ok = true
}
}
return
}
It obviously won't let me use the html token because I can't import the package.
You can execute at the command line “go get golang.org/x/net/html”
You can take a look at https://golang.org/doc/code.html. It contains a very good description on how to start coding in Go. The issues you are mentioning are described there.
So, you set GOROOT to your installation path. Then you set GOPATH to your go workspace(s), which obey the bin, pkg, src structure. The first entry in your GOPATH is used to store the go get ... which you install on your machine.
Related
Imagine I want to use strconv.Atoi, but I am lazy, and want to import it automatically.
package main
import (
"fmt"
)
func main() {
i, _ := Atoi|("123")
fmt.Println(i)
}
The pipe sign (|) shows where my cursor is
In PyCharm I was able to automatically import the matching function via alt+ENTER.
Is there a way that vscode changes above code to this one:
package main
import (
"fmt"
"strconv"
)
func main() {
i, _ := strconv.Atoi("123")
fmt.Println(i)
}
VSCode won't guess which package Atoi() is from, but if you tell it, the import will be added automatically.
So just type
i, _ := strconv.Atoi("123")
And hit CTRL+S to save, and the import will be added automatically.
You may also press CTRL+ALT+O which is a shortcut to Organize Imports.
This is a reasonable compromise in Go. As part of API design, exported identifiers are created that read well with the package name. For example the constructor function that creates an MD5 hasher is md5.New() (and not for example md5.NewMD5()), the one that creates an SHA1 hasher is sha1.New(). Just entering New() it's often too verbose, and giving the package name is required to give context to what you refer to.
I am trying to build a simple webserver. I want to replace all \n with <br>.
I wanted to use bytes for this, because my page body is stored as an []byte.
I use bytes.ReplaceAll() for this. But it keeps saying that it's a reference to undefined identifier.
Can someone tell me why? I tried the exact same line within an online Compiler and it worked just fine. Do I miss the library?
See my code below:
import (
"bytes"
"html/template"
"io/ioutil"
"log"
"net/http"
"regexp"
)
type Page struct {
Title string
Body []byte
}
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
// THE LINE THAT CAUSES TROUBLE
p.Body = bytes.ReplaceAll(p.Body, []byte("\n"), []byte("<br>"))
renderTemplate(w, "edit", p)
}
The bytes package is part of the standard library, so it is unlikely that you don't have it if you have the go tool available to you.
But do note that bytes.ReplaceAll() was added in Go 1.12, so if you have an older Go SDK, this function will not be available to you.
Execute go version to find out. Get the latest Go from the official site: https://golang.org/dl/
Further to icza's answer,
For the benefit of Go versions prior to 1.12, the following are equivalent:
bytes.ReplaceAll(a, b, c)
and
bytes.Replace(a, b, c, -1)
See the implementation of ReplaceAll
I'm following the Go tour, and still trying to learn the basics of the language. For the imported package time, is there a way to access its exports with a variable? E.g. time[day] instead of time.Saturday
Here's a more complete example
package main
import (
"fmt"
"time"
)
func main() {
day := "Thursday"
fmt.Printf("When's %v?", day)
today := time.Now().Weekday()
switch time[day] { // This is how I would do it in javascript
case today + 0:
fmt.Println("Today.")
default:
fmt.Println("Too far away.")
}
}
Also, what is the correct terminology for what I want to do? I'm having very little luck using Google
No, there's no way to reference exported variables from a package, which are stored on the stack, without explicitly referencing them using a data structure that you define that's built at runtime.
For example, you could do:
var days = map[string]time.Weekday{
"Monday": time.Monday,
"Tuesday": time.Tuesday,
"Wednesday": time.Wednesday,
"Thursday": time.Thursday,
"Friday": time.Friday,
"Saturday": time.Saturday,
"Sunday": time.Sunday,
}
fmt.Println(days["Thursday"])
See http://play.golang.org/p/6EYqcklf8X
I have created /app/models/todo-item.go file that looks like this:
package models
import (
"github.com/revel/revel"
)
type TodoItem struct {
Id int64 `db:"id" json:"id"`
Name string `db:"name" json:"name"`
}
func (b *TodoItem) Validate(v *revel.Validation) {
v.Check(b.Name,
revel.ValidRequired(),
revel.ValidMaxSize(25))
}
In src/RevelApp/app/controllers/init.go, I have this (PS, I am using GorpController to interact with MySQL):
func defineTodoItemTable(dbm *gorp.DbMap){
// set "id" as primary key and autoincrement
t := dbm.AddTable(models.TodoItem{}).SetKeys(true, "id")
t.ColMap("name").SetMaxSize(25)
}
I am getting an error :
The Go code src/RevelApp/app/controllers/init.go does not compile: undefined: models
I have tried importing ."RevelApp/app/models" then doing away with models in models.TodoItem{} (as describer here: Revel with Gorm "undefined: Page") and I get the error : App failed to start up
revel/harness: app timed out.
That link is the only one I could find related to this issue. Am I missing something?
EDIT: $GOPATH:
/home/me/Source/go
models location:
/home/me/Source/go/src/RevelApp/app/models
How I am importing models package:
import (
."RevelApp/app/models"
"github.com/revel/revel"
"github.com/coopernurse/gorp"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"fmt"
"strings"
)
You have imported your models using the dot (.) import qualifier. From the language spec:
If an explicit period (.) appears instead of a name, all the package's exported identifiers will be declared in the current file's file block and can be accessed without a qualifier.
This means you don't need to use models.TodoItem .. you can simply use TodoItem.
That said .. I would suggest avoiding this and removing the dot from the import statement:
import (
"RevelApp/app/models"
"github.com/revel/revel"
"github.com/coopernurse/gorp"
"database/sql"
_ "github.com/go-sql-driver/mysql"
"fmt"
"strings"
)
Why? It stops your local file scope becoming crowded and allows you to see where each object actually resides.
For completeness (and you're probably wondering what its for since the dot is similar), that underscore (_) before the mysql package statement basically says "import this package .. but I don't need to use anything in it directly". This fires the packages init function allowing it to register itself with the database/sql packages routines. Which is why when you use the sql.DB type it is eventually routed to the MySql package code.
I'm trying to write an application to pull status from a database, but I seem to be getting stuck on a really basic principle of the language. I have the program written, but it doesn't compile due to the error use of package time not in selector.
A really basic example (from play.golang.org's own test environment)
package main
import (
"fmt"
"time"
)
func main() {
s_str := time.Now()
fmt.Println( printT(s_str) )
}
func printT(t time) time {
return t.Add(100)
}
Unfortunately, I've found documentation and helpdocs online a bit wanting. My understanding is that the import statement should include the library for the entire program like in C++ correct?
You have to prefix the imported types or variables with the name you gave to the package in the import (here you use the default name, that is "time"). That's what you did for the function Now but you have to do it also for the types.
So the type isn't time but time.Time (that is : the type Time that is declared in the package you import with the name "time").
Change your function to
func printT(t time.Time) time.Time {
return t.Add(100)
}
And for your second question : No, the import statement doesn't include the library for the entire program but only for the current file.