go


How do I serve CSS and JS in Go Lang


I followed the GoLang tutorial here but for whatever reason I am having trouble getting the app to serve css and js. If I run my static page without the go server the page css works fine. When I run the go server on the other hand the css just doesn't work.
Here is what my html sort of looks like:
<link rel="stylesheet" href="../assets/css/bootstrap.min.css">
<link rel="stylesheet" href="../assets/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../assets/css/custom.css">
.
.
.
then under the body tag
<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
.
.
.
My file tree looks like this
How do I get my go application to serve the css and javascript I need?
The problem has since been solved, here is the working main:
func main() {
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/index/", makeHandler(indexHandler))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil)
}
Here is an example of the handlers I am using
func indexHandler(w http.ResponseWriter, r *http.Request, title string) {
p := &Page{Title: title}
err := templates.ExecuteTemplate(w, "index.html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
You can serve a directory of static files like so:
http.Handle("/", http.FileServer(http.Dir("css/")))
Would serve your css directory at /. Of course you can serve whichever directory at whatever path you choose.
I would probably make sure that the static path isn't in the way of other paths and use something like this.
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
Placing both your js and css in the directory static in your project. This would then serve them at domain.com/static/css/filename.css and domain.com/static/js/filename.js
The StripPrefix method removes the prefix, so it doesn't try to search e.g. in the static directory for static/css/filename.css which, of course, it wouldn't find. It would look for css/filename.css in the static directory, which would be correct.

Related Links

Appending to multiple instances of a slice
How to apply range on multiple slices in golang Beego template?
How to type convert a slice of type aliases
How to set consistency option in golang etcd client
go http template extra data leaking in the template
What's the point of creating one-way channels in Go
Docker Machine - generic driver does not support start
Why redefining a variable does not always trigger an error?
Functions of interface type returning struct of (another) interface type
How to return a string from JSON to a C caller (Golang CGO)?
How to get an actual file from a blob with git2go / libgit2?
Name for a generated go file
Assigning a variable of reference type “slice” to another variable, why don't they change simultaneously?
Godeps development flow?
How to write these mongo code in go
Type alias for channel in Go works strange

Categories

HOME
sonarqube
microsoftgraph
oracle12c
gridview
delphi-7
cheerio
dns
alexa-skills-kit
themes
yahoo-finance
jelastic
yandex
polling
google-cloud-datalab
banner
boto3
title
character
teechart
heap
why3
php-5.6
comsol
plink
magnetic-cards
do-while
parameter-passing
internationalization
entitymanager
intershop
mathprog
android-7.0-nougat
video-editing
foreign-keys
bourbon
simpleitk
symbols
poedit
hta
jboss6.x
jackson-modules
8051
tpm
panoramas
anki
sql-server-2000
simplecv
hunspell
sharepoint-workflow
raft
metawidget
azure-arm
stringtokenizer
rfc3339
reportservice2010
dtsearch
lightning
corruption
carrot2
context-switch
stdmap
modelsim
subnet
er-diagram
grails-plugin
microstation
segment-io
installshield-2010
dup
cifilter
kango-framework
angular-ui-typeahead
phpgrid
rasterize
opl
themoviedb-api
rewrite
computer-architecture
rate-limiting
ember-cli-rails
baasbox
ansi
google-refine
procedural-programming
human-computer-interface
rmysql
ss7
tinyxml
rfc5322
oocss
route-provider
twisted.web
web-farm
inkcanvas
database-create
ilasm
entity-attribute-value
jquery-tools
fsevents
cgbitmapcontextcreate
collectionviewsource
metaweblog
visualj#
application-planning
unmanagedresources
cardspace
great-circle

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App