This is an example of how to get GO annotation by Ensembl Gene IDs.
# install biomaRt, if not source('http://bioconductor.org/biocLite.R')
# biocLite('biomaRt')
library(biomaRt)
ensembl_gene_ids <- c("ENSG00000163734", "ENSG00000174348")
mart <- useMart(biomart = "ensembl", dataset = "hsapiens_gene_ensembl")
goids <- getBM(attributes = c("ensembl_gene_id", "go_id"), filters = "ensembl_gene_id",
values = ensembl_gene_ids, mart = mart)
write.table(x = goids, file = "gene_to_go.tab", sep = "\t", quote = FALSE,
row.names = FALSE, col.names = FALSE)